Skip to content
Andesine

SDK

Use the Andesine API from TypeScript with the @andesine/sdk package.

@andesine/sdk is the TypeScript client for the Andesine API. It works in Node.js 22 or later, and in browsers and workers. It is open source under the MIT license.

sh

Create a client

ts

Without apiKey, the client reads ANDESINE_API_KEY from the environment. baseURL defaults to https://api.andesine.app; set it for a self-hosted instance.

Each operation of the API reference is a method, such as client.entries.get() or client.publishing.publishEntry(). Methods take one object with all the fields, and return the data.

In the browser

Use a publishable key in browser code:

ts

A publishable key can read, search, and, if allowed, answer questions about the published content of its collections. It works only on its allowed origins. See API overview.

For a user

To act for a signed-in user, pass an OAuth access token, and the workspace in a header:

ts

The SDK does not sign in users or refresh tokens.

Structured content

toStructuredContent() returns the properties and fragments of an entry as two maps, keyed by field:

ts

For typed fields, generate content types.

Read a consistent set of pages

Pin related reads to the snapshot of the first read, so all pages come from the same publish:

ts

To list entries with their content, filtered by properties:

ts

Pagination

ts

Use paginatePages() to work page by page and save the cursor.

AI answers

ts

[1] in the answer refers to the source with id 1. Call AI answers from your server with a secret key, or from the browser with a publishable key that allows AI answers. Each answer uses 10 AI credits.

Stream an answer

askPublishedStream and askCurrentStream send the answer while it is written. They take the same input as askPublished and askCurrent:

ts

The stream sends one sources event, then textDelta events, then one completed event with the full answer. The text is plain text that can contain Markdown. An answer without sources can complete without textDelta events.

  • To stop, call controller.abort() or break from the loop. If you do not read the stream, call await events.return?.() to close it.

  • The timeout is for the full stream. timeout: 0 turns it off.

  • The SDK does not reconnect or retry a stream.

  • An error before the stream starts is an AndesineAPIError. An error during the stream is an AndesineStreamError. A stream that is not complete or not valid gives an AndesineStreamProtocolError.

An answer uses credits when the generation starts, also if it fails or you cancel it later.

Forward a stream to the browser

Keep your secret key on your server. @andesine/sdk/streaming forwards the stream from your server to the browser:

ts

toAnswerResponse uses only standard Fetch types and sends no headers or credentials from Andesine. Handle errors that occur before it with your normal error handler. Your hosting platform must support streamed responses.

In the browser, read the response with readAnswerStream:

ts

Errors

Failed requests throw AndesineAPIError with status, code, message, hints, and data:

ts

Timeouts and retries

Requests time out after 30 seconds; set timeout to change it, or 0 to turn it off. Retries are off by default. Set retries: 2 to retry reads and safe writes after network errors and 429 or 5xx responses. Writes that create or update entries and versions send an idempotency key, so a retry never applies them twice.

Webhooks

client.webhooks manages webhooks, and @andesine/sdk/webhooks verifies incoming events. See Webhooks.

Last updated