Install the SDK
Install @werft-ai/sdk and create a client with a server address and an API key
You use Werft through the TypeScript SDK @werft-ai/sdk. The SDK connects to a running Werft server with an API key. On this page you install the SDK, create a client with the server address and a key, and check the connection.
Prerequisites
| Item | Description |
|---|---|
| Server address | The URL of the Werft server, for example https://werft.example.com. The development server is http://127.0.0.1:3000 |
| API key | A key that starts with csp_. Creating a session needs sessions:read and sessions:write; answering permission requests and closing a session also need sessions:approve and sessions:control |
| Node 20.3 or later, or Bun | The runtime the SDK runs on |
If you have no server, start the development server in Run a server and get an API key. For a server in operation, issue the key in the web console (API key).
Install
Create an app directory and install @werft-ai/sdk from npm.
mkdir my-werft-app && cd my-werft-app
npm init -y
npm pkg set type=module
npm install @werft-ai/sdkThis command installs the release (dist-tag latest). Automatic prerelease publishing on main pushes has stopped. The existing main dist-tag is no longer updated, so use a release version.
The SDK package is under the Apache License 2.0. The server and the rest of the repository are source-available code under the PolyForm Small Business License 1.0.0.
Create a client
Pass the server address (baseUrl) and the API key (apiKey) when you create Werft. The server address has no default. Do not write the key in code; read it from an environment variable.
import { Werft } from "@werft-ai/sdk";
export const werft = new Werft({
baseUrl: "http://127.0.0.1:3000",
apiKey: process.env.WERFT_API_KEY,
});baseUrlis the server's address. Do not append/v1.- Leave an option out and the client reads the
WERFT_BASE_URLorWERFT_API_KEYenvironment variable. If either is missing,new Werft()throws aTypeError. http://to an address other than loopback is refused, because the key would cross the network in clear text. Connect to a remote server overhttps://.- You cannot create a client in a browser, because a key in a page leaks.
The remaining options, such as retries, timeouts and error types, are in Client settings and errors.
Check the connection
Read the key's owner and scopes, and the profiles registered on the server.
import { werft } from "./werft.ts";
const me = await werft.auth.me();
console.log(me.principal.kind, me.scopes);
const { items } = await werft.profiles.list();
for (const profile of items) console.log(profile.id, profile.repository_mode);WERFT_API_KEY=csp_… node check.tsapi_key [
'sessions:read',
'sessions:write',
'sessions:approve',
'sessions:control',
'sessions:recover'
]
claude-coding-auto required
claude-coding-local required
general noneNode 22.18 or later and Bun run .ts files directly. On an older Node, run npx tsx check.ts. A wrong key raises APIError (401), and a server that cannot be reached raises ConnectionError.
Calling the HTTP API
Without the SDK, add an Authorization: Bearer <key> header to every request. The curl examples in these docs keep the server address in the API shell variable and the key in KEY.
curl -sS -w '\n' "$API/v1" -H "Authorization: Bearer $KEY"The endpoints are in the HTTP API reference.
Next steps
- Create your first session: create a session with the SDK and read the model's answer