AI

AI with Node.js and TypeScript

This page shows practical examples for calling Zenifra's AI API in Node.js and TypeScript applications using OpenAI-compatible clients.

Prerequisites

  • a key created in AI Models in the console
  • budget configured for the key
  • model enabled for that key
  • Node.js 18+ or a runtime compatible with fetch

Store the key in an environment variable:

export ZENIFRA_AI_API_KEY="your-key"

Example with the OpenAI SDK

Install the SDK:

npm install openai

Create ai-example.ts:

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://ai.zenifra.com/v1',
  apiKey: process.env.ZENIFRA_AI_API_KEY,
});

async function main() {
  const response = await client.chat.completions.create({
    model: 'zenifra/qwen3.6-35b-a3b',
    messages: [
      {
        role: 'user',
        content: 'Summarize what a PaaS is in three bullet points.',
      },
    ],
  });

  console.log(response.choices[0]?.message?.content);
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Replace the model with the name enabled for your key in the console.

Example with curl

curl https://ai.zenifra.com/v1/chat/completions \
  -H "Authorization: Bearer $ZENIFRA_AI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "zenifra/qwen3.6-35b-a3b",
    "messages": [
      { "role": "user", "content": "Explain containers in one sentence." }
    ]
  }'

Error handling

Handle common errors before shipping the integration to production.

try {
  const response = await client.chat.completions.create({
    model: 'zenifra/qwen3.6-35b-a3b',
    messages: [{ role: 'user', content: 'Hello' }],
  });

  console.log(response.choices[0]?.message?.content);
} catch (error: any) {
  const status = error?.status;

  if (status === 401) {
    throw new Error('AI key is invalid or missing.');
  }

  if (status === 403) {
    throw new Error('Model is not allowed for this key.');
  }

  if (status === 429) {
    throw new Error('Limit exceeded. Try again in a few seconds.');
  }

  throw error;
}

Production usage

  • use an environment variable for the key
  • limit each key by budget
  • separate keys by environment or integration
  • log only IDs, status, and timings; do not log sensitive prompts unnecessarily
  • review costs and usage by model in the console

Next steps

FAQ

Can I use JavaScript without TypeScript?

Yes. The same example works in JavaScript by removing types and using import or require according to your project.

Do I need to use the SDK?

No. Any HTTP client can call the OpenAI-compatible endpoint.

Where do I see tokens and cost?

In the Zenifra console, in the AI Models area.