TypeScript SDK
Prerequisites
| Item | Value |
|---|---|
| Package | @joytoken/client-sdk-ts |
| Node.js | 18+ |
| API key | JOY_TOKEN_API_KEY |
| API Base URL | JOY_TOKEN_API_BASE_URL from Environments |
| OpenAI Base URL | JOY_TOKEN_OPENAI_BASE_URL from Environments |
Step 1: Install
$ npm install @joytoken/client-sdk-ts
Step 2: Create a Client
1 import { JoyTokenClient } from "@joytoken/client-sdk-ts"; 2 3 const joytoken = new JoyTokenClient({ 4 apiKey: process.env.JOY_TOKEN_API_KEY, 5 apiBaseUrl: process.env.JOY_TOKEN_API_BASE_URL, 6 openAIBaseUrl: process.env.JOY_TOKEN_OPENAI_BASE_URL, 7 timeoutMs: 60_000, 8 });
Step 3: Chat Completion
1 const completion = await joytoken.chat.completions.create({ 2 model: "auto", 3 messages: [{ role: "user", content: "Reply with exactly: pong" }], 4 }); 5 6 console.log(completion.choices[0]?.message?.content);
Step 4: Streaming Response
1 for await (const chunk of joytoken.chat.completions.stream({ 2 model: "auto", 3 messages: [{ role: "user", content: "Write one short paragraph." }], 4 })) { 5 process.stdout.write(chunk.choices[0]?.delta?.content ?? ""); 6 }
Step 5: Model List
1 const models = await joytoken.models.list(); 2 console.log(models.data.map((model) => model.id));
Common Errors
| Error | Fix |
|---|---|
No fetch implementation available | Use Node.js 18+, or pass fetch to JoyTokenClient({ fetch }) |
401 Unauthorized | Check JOY_TOKEN_API_KEY |
JoyTokenAPIError | Inspect status, requestId, and body |