# SDK

[@layer\_ai/sdk](https://www.npmjs.com/package/@layer%5Fai/sdk) is Layer’s official JavaScript and TypeScript SDK, published to npm. It reaches the same models, workspaces, and Creative Unit balance as the app, from Node, a build script, a game-tooling service, or an agent.

**npm**

```bash
npm install @layer_ai/sdk
```

**pnpm**

```bash
pnpm add @layer_ai/sdk
```

**yarn**

```bash
yarn add @layer_ai/sdk
```

Then generate:

```ts
import { Layer } from '@layer_ai/sdk'


const layer = new Layer({ apiKey: process.env.LAYER_API_KEY })


const run = await layer.inferences.generate({
  prompt: 'a crystal sword icon, transparent background',
})


console.log(run.outputs?.[0]?.url)
```

`generate()` prices the run, submits it, and polls until it finishes. Leave `base_model_id` off and Layer picks the recommended model for the modality; the finished run reports which one it used.

[Authentication](/docs/sdk/authentication)Personal Access Tokens, OAuth access tokens, and choosing a workspace.

[Generating assets](/docs/sdk/generate)Images, video, 3D, and audio — pricing, waiting, references, and uploads.

[Errors & retries](/docs/sdk/errors)What the SDK throws, what it retries, and why a retry never pays twice.

Pin the version in CI so a release cannot change a build under you:

```bash
npm install @layer_ai/sdk@1.0.0
```

## Requirements

Node 20.6 and up, or any runtime with a global `fetch` — Bun, Deno, Cloudflare Workers, and modern browsers included. The package is ESM only, ships its own TypeScript types, and has no dependencies.

Running in a browser puts your credential in the browser. Use a short-lived OAuth access token there, never a Personal Access Token; see [authentication](/docs/sdk/authentication).

## What it covers

Every namespace maps to a resource of the [v2 REST API](/docs/v2/rest-api).

| Namespace           | What it reaches                                                        |
| ------------------- | ---------------------------------------------------------------------- |
| layer.inferences    | Generation: estimate, create, wait, cancel                             |
| layer.baseModels    | The model catalogue, Layer’s defaults, and per-model parameter schemas |
| layer.referenceSets | Trained subjects, styles, and characters                               |
| layer.trainingRuns  | Training a reference set, and the model it produces                    |
| layer.files         | Direct-to-storage uploads and per-file quality scores                  |
| layer.workflows     | Saved multi-step Blueprints and their runs                             |
| layer.projects      | Projects and their members                                             |
| layer.workspaces    | Workspaces, balance, and usage                                         |
| layer.scores        | Output scoring rules, and running them over files                      |

Anything the SDK has no method for is still one call away through the transport, which keeps the authentication, retries, and error handling:

```ts
await layer.http.get('/v2/workspaces/…/groups')
```

## Other ways in

[CLI](/docs/cli)The same surface from a terminal, a Makefile, or a CI job.

[REST API](/docs/v2/rest-api)Every endpoint, for any language.

[MCP server](/docs/mcp)Layer as tools inside an AI assistant.
