> ## Documentation Index
> Fetch the complete documentation index at: https://docs.near.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript

> Use the JavaScript SDK for verified, end-to-end encrypted Chat Completions.

## Install

The SDK is available on [npm](https://www.npmjs.com/package/@nearai/inference-sdk). It requires Node.js 24 or later for Node usage.

```bash theme={"dark"}
npm install @nearai/inference-sdk
```

Set your API key in the environment:

```bash theme={"dark"}
export NEARAI_API_KEY="your-api-key"
```

Save the example below as `chat.mjs` and run it with `node chat.mjs`.

## E2EE

End-to-end encryption (E2EE) is disabled by default in `InferenceClient`. Set `e2ee: true` to enable it for a model with supported model attestation. When enabled, the SDK verifies the required attestation evidence, encrypts supported Chat fields to a key from the verified model evidence, and decrypts the response automatically.

```js theme={"dark"}
import { InferenceClient } from '@nearai/inference-sdk/node';

const baseUrl = 'https://cloud-api.near.ai/v1';
const model = 'z-ai/glm-5.3-flash';
const apiKey = process.env.NEARAI_API_KEY;
if (!apiKey) throw new Error('NEARAI_API_KEY is required');

const client = new InferenceClient({
  baseUrl,
  apiKey,
  e2ee: true,
});

const completion = await client.chat.completions.create({
  model,
  messages: [{ role: 'user', content: 'Reply with the word ok.' }],
});

// Verify the response signature before displaying the answer.
const verified = await client.verifyResponse(completion.id);
console.log(`Verified ${verified.signatureKind} response.`);
console.log(completion.choices[0]?.message.content);
```

## Verification

`InferenceClient` verifies the Gateway and every returned NEAR model-attestation candidate before sending an E2EE Chat Completion. Call `verifyResponse()` after a completion to verify its response signature with the request and response bytes captured by the client.

```js theme={"dark"}
const receipt = await client.verifyResponse(completion.id);
console.log(`Verified ${receipt.signatureKind} response.`);
```

<Note>
  `verifyResponse()` is explicit. For a streaming completion, consume the entire stream first, then call it with the completion ID so the SDK can verify the exact response bytes.
</Note>

### Set a TCB policy

By default, the SDK accepts the Intel TCB statuses `UpToDate` and `OutOfDate`. To require `UpToDate`, apply a policy to both the Gateway and model evidence:

```js theme={"dark"}
const strict = {
  policy: { acceptedTcbStatuses: ['UpToDate'] },
};

const client = new InferenceClient({
  baseUrl,
  apiKey,
  e2ee: true,
  gatewayVerification: strict,
  modelVerification: strict,
});
```

### Runtime behavior

For Node, import from `@nearai/inference-sdk/node`. It verifies the Gateway TLS peer and binds later requests to the verified Gateway identity. For a browser or application proxy, import from `@nearai/inference-sdk`; browser Fetch cannot read the TLS peer certificate, so that entry point does not perform Gateway TLS binding.

For a manual verification flow or a custom policy, see [Verification](/cloud/verification).

## OHTTP

Set `ohttp: true` to encapsulate Chat requests and responses with OHTTP. OHTTP uses the SDK's default Ed25519 signing algorithm; it is not compatible with `signingAlgo: 'ecdsa'`.

```js theme={"dark"}
const client = new InferenceClient({
  baseUrl,
  apiKey,
  e2ee: true,
  ohttp: true,
});
```

The Chat and `verifyResponse()` calls stay the same. Before sending Chat, the SDK verifies the Gateway's signed OHTTP configuration; a missing or invalid configuration blocks the request. OHTTP encapsulates the exchange to the Gateway, while E2EE encrypts supported Chat fields to the verified model key.

<Note>
  OHTTP applies only to Chat. Attestation and signature requests remain regular HTTPS. The configured endpoint or proxy must expose `/ohttp` at the same origin. Authorization and custom headers sent on the outer request, along with the client's network address, are not hidden by OHTTP.
</Note>

## Examples

See the [Inference SDK examples and setup instructions](https://github.com/nearai/inference-sdk/tree/main/examples) for runnable projects.

* [InferenceClient](https://github.com/nearai/inference-sdk/blob/main/examples/example-js/gateway/client.ts): streaming and non-streaming Chat, response verification, and Gateway image provenance checks.
* [OpenAI SDK integration](https://github.com/nearai/inference-sdk/blob/main/examples/example-js/gateway/client-openai-sdk.ts): use `InferenceClient.fetch` with the OpenAI client.
* [Manual verification and E2EE](https://github.com/nearai/inference-sdk/blob/main/examples/example-js/gateway/bare.ts): work directly with attestation, encryption, and signature-verification helpers.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.