AI client
Azion Lib functions of the azion/ai module that send questions about Azion to the Azion AI API and return the answer whole or as a stream.
The azion/ai module of the azion package is the Azion Lib client for the Azion AI API, an assistant that answers questions about Azion products and services. You send it a conversation, and it returns the answer in one piece with chat or in chunks, as the model writes it, with streamChat.
Install the package:
The azion package receives bug fixes only, and its maintenance ends in December 2026.
Every sample on this page is a TypeScript ES module that runs in Node.js and uses top-level await. Types come in through import type, so the samples still load when a tool strips the type annotations before running them.
Authentication
The chat and streamChat functions take your personal token from the AZION_TOKEN environment variable. A client from createClient holds its own token, passed in the token field.
A .env file with both variables looks like this:
| Variable | Description |
|---|---|
AZION_TOKEN | Your Azion personal token. |
AZION_DEBUG | With true, chat prints the whole response object before the answer, and streamChat prints every chunk object. |
For how the Azion Lib packages resolve the token and the debug setting, refer to How Azion Lib works.
Response envelope
Every call returns an AzionAIResult object, { data, error }. On success, data holds the response and error is null. On failure, data is null and error is a JavaScript Error. A refused call does not throw.
Read the failure from error.message. The message property of an Error is not enumerable, so JSON.stringify(error) prints {} and hides the reason.
createClient
Creates a client that holds a token and request options and exposes chat and streamChat as methods. createClient is also the default export of azion/ai.
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | No | Your Azion personal token. |
options | AzionClientOptions | No | Request options for every call the client makes. |
Returns an AzionAIClient. Its methods take the same arguments as the chat and streamChat functions.
This sample creates a client and asks it one question:
The model writes a different answer on every run. This output is cut after the answer, before the list of documentation pages:
chat
Sends a conversation to the Azion AI API and returns the whole answer when the model finishes it. The function takes two positional arguments, not one object.
| Parameter | Type | Required | Description |
|---|---|---|---|
request | AzionAIRequest | Yes | The conversation to send. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionAIResponse. The answer is in choices[0].message.content.
On success, the sample prints AI response: followed by the answer.
streamChat
Sends a conversation to the Azion AI API and yields the answer in chunks while the model writes it. Use it to show the answer as it arrives.
| Parameter | Type | Required | Description |
|---|---|---|---|
request | AzionAIRequest | Yes | The conversation to send. |
options | AzionClientOptions | No | Request options. |
Returns an async generator, not a promise: read it with for await. Each chunk is its own envelope, with data as an AzionAIStreamResponse. The text of the chunk is in choices[0].delta.content, which a chunk can leave out.
The model writes a different answer on every run. This output is cut after the first use case:
Errors
A failed call returns an Error in error. Read its text from error.message.
| Message | Cause | What to do |
|---|---|---|
HTTP error! status: 403 | The Azion AI API refused the token. | Use a valid personal token in AZION_TOKEN or in the client token. |
Types
The module exports these types. Import them with import type.
AzionAIClient
The client that createClient returns.
| Method | Arguments | Returns |
|---|---|---|
chat | request: AzionAIRequest, options?: AzionClientOptions | Promise<AzionAIResult<AzionAIResponse>> |
streamChat | request: AzionAIRequest, options?: AzionClientOptions | AsyncGenerator<AzionAIResult<AzionAIStreamResponse>> |
AzionClientOptions
Request options that chat and streamChat take in options, and createClient takes for all its calls.
| Property | Type | Required | Description |
|---|---|---|---|
debug | boolean | No | Prints the whole response object, or every chunk object of a stream, before the answer. |
force | boolean | No | — |
AzionAIRequest
The conversation that chat and streamChat send.
| Property | Type | Required | Description |
|---|---|---|---|
messages | AzionAIMessage[] | Yes | The messages of the conversation, in order. |
azion | AzionAIConfig | No | — |
stream | boolean | No | — |
AzionAIMessage
One message of the conversation.
| Property | Type | Required | Description |
|---|---|---|---|
role | 'system' | 'user' | 'assistant' | Yes | The author of the message. |
content | string | Yes | The text of the message. |
AzionAIConfig
The settings a request carries in azion. Every field is an optional string.
AzionAIResult
The envelope every call returns, and every chunk of a stream. For how to read it, refer to Response envelope.
| Property | Type | Required | Description |
|---|---|---|---|
data | T | null | Yes | The response, or null when the call fails. |
error | Error | null | Yes | The error, or null when the call succeeds. |
AzionAIResponse
The response of chat. The answer is in choices[0].message.content.
AzionAIStreamResponse
One chunk of streamChat. The text of the chunk is in choices[0].delta.content.