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.
Create a client
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:
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:
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:
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:
To list entries with their content, filtered by properties:
Pagination
Use paginatePages() to work page by page and save the cursor.
AI answers
[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:
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()orbreakfrom the loop. If you do not read the stream, callawait events.return?.()to close it.The timeout is for the full stream.
timeout: 0turns 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 anAndesineStreamError. A stream that is not complete or not valid gives anAndesineStreamProtocolError.
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:
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:
Errors
Failed requests throw AndesineAPIError with status, code, message, hints, and data:
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.