TypeScript API Reference

Import

1import {
2 JoyTokenAPIError,
3 JoyTokenClient,
4 type ChatCompletionRequest,
5 type ChatCompletionResponse,
6 type ChatCompletionChunk,
7 type ChatMessage,
8 type ChatTool,
9 type JoyTokenClientOptions,
10 type ModelListResponse,
11} from "@joytoken/client-sdk-ts";

JoyTokenClient

1new JoyTokenClient(options?: JoyTokenClientOptions)
OptionTypeDescription
apiKeystringJoyToken API key. Defaults to JOY_TOKEN_API_KEY
apiBaseUrlstringBase URL for non-OpenAI endpoints. Defaults to https://api-dev.joytokens.ai
openAIBaseUrlstringBase URL for Chat Completions. Defaults to ${apiBaseUrl}/openai/v1
fetchtypeof fetchCustom fetch implementation
defaultHeadersRecord<string, string>Headers added to every request
timeoutMsnumberRequest timeout in milliseconds

chat.completions.create

1client.chat.completions.create(request: ChatCompletionRequest): Promise<ChatCompletionResponse>

Sends a non-streaming request to POST /openai/v1/chat/completions.

Required fields:

FieldType
modelstring
messagesChatMessage[]

Common optional fields include temperature, max_tokens, top_p, stop, tools, tool_choice, tier, and metadata.

chat.completions.stream

1client.chat.completions.stream(request): AsyncIterable<ChatCompletionChunk>

Sends a streaming request to POST /openai/v1/chat/completions with stream: true.

1for await (const chunk of client.chat.completions.stream(request)) {
2 // chunk.choices contains assistant deltas
3 // chunk.metadata contains JoyToken metadata when present
4}

models.list

1client.models.list(): Promise<ModelListResponse>

Calls GET /api/v1/models and returns:

1interface ModelListResponse {
2 object?: string;
3 data: ModelInfo[];
4}

JoyTokenAPIError

Non-2xx responses throw JoyTokenAPIError.

FieldType
statusnumber
requestId`string
responseHeadersHeaders
bodyunknown

Core types

1type ChatRole = "system" | "user" | "assistant" | "tool";
2
3interface ChatMessage {
4 role: ChatRole;
5 content?: string | Array<Record<string, unknown>> | null;
6 name?: string;
7 tool_call_id?: string;
8 tool_calls?: ToolCall[];
9}
1interface ChatTool {
2 type: "function";
3 function: {
4 name: string;
5 description?: string;
6 parameters?: Record<string, unknown>;
7 };
8}