Skip to main content

Use the Lattice SDK for gRPC in JavaScript

This page shows you how to set up the Lattice SDK for gRPC in JavaScript and make a gRPC request.

Click to go to the gRPC SDK repository. By downloading and/or using, you agree to the terms of use. If you do not agree, do not use the SDK.

Before you begin

Before you install and use the gRPC SDK in Java, do the following:

  1. Complete the setup steps to generate a valid API token and publish at least one entity to your environment.
  2. Install the latest version of NodeJS.
  3. Install the TypeScript compiler.

Set up your project folder

  1. Create a new folder for your project.
  2. In your project folder, set up packages and install type definitions for Node.js:
    npm init

Install the SDK

To install the gRPC SDK in JavaScript, add the following dependencies to your project:

npm install @anduril-industries/lattice-sdk
npm install @connectrpc/connect
npm install @connectrpc/connect-node

Connect to Lattice APIs

Establish a gRPC channel to connect to Lattice APIs.

Define a new class MyEntityManagerClient that establishes a secure gRPC channel and stores an authenticated Entities API stub:

  1. Create the Entities API promise client:


    import { EntityManagerAPI } from "@anduril-industries/lattice-sdk/src/anduril/entitymanager/v1/entity_manager_grpcapi.pub_pb.js"
    import { createGrpcTransport } from "@connectrpc/connect-node";
    import { createClient } from "@connectrpc/connect";

    const transport = createGrpcTransport({
    // Requests will be made to <baseUrl>/<package>.<service>/method
    baseUrl: "$YOUR_LATTICE_URL",
    });

    async function main() {
    // Create the client instance
    const client = createClient(EntityManagerAPI, transport);
    }

    void main();
  2. Create authorization headers to pass in to each request, replacing $YOUR_BEARER_TOKEN with the bearer token that you generated.


async function main() {
// Create the client instance
const client = createClient(EntityManagerAPI, transport);

// Set authentication
const headers = new Headers();
headers.set("Authorization", "Bearer $YOUR_BEARER_TOKEN");
}

void main();

Allow self-signed certificates

warning

Make sure you understand the implications of using self-signed certificates. This should only be used for testing.

To connect to a server that is using a self-signed certificate, add the following code:

const transport = createGrpcTransport({
// Requests made to <baseUrl>/<package>.<service>/method
baseUrl: "$YOUR_LATTICE_URL",
nodeOptions: {
rejectUnauthorized: false,
}
});

Example request

The following example combines the code snippets from above and makes a GetEntity request to the Entities API to fetch an entity from the Common Operating Picture:


import { EntityManagerAPI } from "@anduril-industries/lattice-sdk/src/anduril/entitymanager/v1/entity_manager_grpcapi.pub_pb.js"
import { createGrpcTransport } from "@connectrpc/connect-node";
import { createClient } from "@connectrpc/connect";

const transport = createGrpcTransport({
// Requests will be made to <baseUrl>/<package>.<service>/method
baseUrl: "$YOUR_LATTICE_URL",
});

async function main() {
// Create the client instance
const client = createClient(EntityManagerAPI, transport);

// Set authentication
const headers = new Headers();
headers.set("Authorization", "Bearer $YOUR_BEARER_TOKEN"); // Replace $YOUR_BEARER_TOKEN with your token. You must keep the word, Bearer, in the metadata string.

// Get an entity's information
const response = await client.getEntity(
{ entityId: "$ENTITY_ID" },
{ headers: headers },
);
console.log(response);
}

void main();

If the request is successful and you replaced the entity ID then you should see the entity object you created when setting up your development environment. If you did not change the entity ID to a valid, existing entity, then you will get the following error:

ConnectError: [not_found] entity not found ENTITY_ID

This means you successfully authenticated and completed the call to your environment, but the entity does not exist. This is an expected error. If you get any other error then there is a problem with your code. Check that your Lattice environment URL and our bearer token are correct.

What's next