@to11ai/sdk package gives TypeScript and JavaScript applications typed access to the to11 platform API. You use it to manage prompts, prompt versions, project environments, and routing rules from your own code.
The SDK talks to the to11 platform API — the control plane. It does not send LLM or chat traffic. There is no .chat() method. To route model calls, you point a provider SDK (OpenAI, Anthropic, or the Vercel AI SDK) at the to11 gateway at https://gw.to11.ai/v1. See Integrations for that path.
The package is published under the MPL-2.0 license. The current version is 0.4.0.
Installation
The package runs in Node (both ESM and CommonJS), Bun, Deno, and browser and edge runtimes. No minimum Node version is declared. Install@to11ai/sdk 0.4.0 or later.
Authentication
The SDK authenticates to the to11 API with a bearer token. It sets the standardAuthorization: Bearer <to11-api-key> header for you from the credential you supply. The project is carried in the URL path, not in a header.
Supply exactly one of two credential options when you construct the client:
apiKey— a to11 API key, for server-side use. The SDK throws if you passapiKeyin a browser context.getAccessToken— an async function that returns a short-lived token, for browser and edge runtimes. The SDK calls it once per request.
Use
getAccessToken instead of apiKey in any code that runs in the browser. Passing a long-lived apiKey to browser code throws, and would expose the key to end users.Creating a client
createClient(options) is a factory function that returns a client. Call it directly; it is not a class, so do not use new.
| Option | Type | Default | Description |
|---|---|---|---|
baseUrl | string | — | Required. The to11 API base URL, https://api.to11.ai. |
apiKey | string | — | A to11 API key for server-side use. Throws if used in a browser. Supply this or getAccessToken. |
getAccessToken | () => Promise<string> | — | Returns a short-lived token per request, for browser and edge runtimes. Supply this or apiKey. |
projectId | string | — | Required for prompts.fetch(). Validated non-empty. |
env | string | — | Default serving environment for prompts.fetch(), for example production. Must match ^[a-z0-9-]{1,63}$. |
cache | object | { ttlSeconds: 300, maxEntries: 100 } | Response cache configuration. |
timeoutMs | number | 30000 | Per-request timeout in milliseconds. |
maxRetries | number | 3 | Maximum retry attempts for retryable failures. |
prompts, projectEnvironments, and routingRules, plus the method resolveBehavior(key, codedDefault) for resolving an SDK behavior flag.
Resolving prompts
client.prompts.fetch(slug, options?) is the primary, environment-aware way to resolve a prompt for use at runtime. It resolves the prompt addressed by slug against the client’s projectId and env, so both must be set on the client.
client.prompts.resolve(...) is deprecated. Use fetch instead.prompts namespace also covers prompt lifecycle and labeling. Each method takes a single options object.
- Prompts:
create,list,get,update,archive. - Versions:
createVersion,listVersions,getVersion. - Labels on a prompt:
moveLabel,listLabels. - Project labels:
listProjectLabels,createProjectLabel,getProjectLabel,updateProjectLabel,deleteProjectLabel,listProjectLabelUsages,listLabelEvents.
Managing routing rules
client.routingRules groups two nested resources.
client.routingRules.versions—create,list,get. Manages the versioned definitions of a routing rule.client.routingRules.releases—set,rollback,list. Controls which rule version is released, and can roll back to a prior one.
Project environments
client.projectEnvironments manages the environments a project serves into. It exposes list, get, create, update, archive, and restore. Each method takes a single options object.
Error handling
The SDK throws typed errors. All extendTo11aiError. API errors extend To11aiApiError, which carries a statusCode. Catch the specific class you care about and fall back to the base classes for the rest.
| Error | Extends | Raised when |
|---|---|---|
To11aiError | — | Base class for all SDK errors. |
To11aiApiError | To11aiError | Base for API errors. Carries statusCode. |
To11aiValidationError | To11aiApiError | The API rejected the request (400). |
To11aiAuthError | To11aiApiError | Authentication or authorization failed (401 or 403). |
To11aiRateLimitError | To11aiApiError | Rate limited (429). Carries retryAfterSeconds. |
To11aiNetworkError | To11aiError | A network failure or timeout occurred. |
To11aiResponseValidationError | To11aiError | A 2xx response body failed schema validation. |
PromptNotFoundError | To11aiApiError | The requested prompt does not exist. |
PromptLabelNotRegisteredError | To11aiApiError | The referenced prompt label is not registered. |
NoActiveReleaseError | To11aiApiError | No release is active for the resolution target. |
PromptPolicyViolationError | To11aiApiError | A prompt policy was violated. |
EnvNotFoundError | To11aiApiError | The referenced environment does not exist. |
EnvNotEnabledForProviderError | To11aiApiError | The environment is not enabled for the provider. |
AmbiguousEnvLabelsError | To11aiApiError | The environment labels resolve ambiguously. |
RuleNotFoundError | To11aiApiError | The requested routing rule does not exist. |
NoApplicableRouteError | To11aiApiError | No route applies to the request. |
RuleVersionInvalidSnapshotError | To11aiApiError | The rule version snapshot is invalid. |
Next steps
Python SDK
The to11 Python SDK.
API reference
The underlying HTTP API.