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
- daily and monthly quota configured for the key, if you want to cap spending
- 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"Permissions
The inference call uses the AI credential created in the console, not an organization API Key. The ai_key.create, ai_key.read, ai_key.update, ai_key.delete, and ai_usage.read scopes administer the key and read its usage; they are not sent to the inference endpoint.
Example with the OpenAI SDK
Install the SDK:
npm install openaiCreate 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.8-flash',
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 a catalog ID (GET https://ai.zenifra.com/v1/models) enabled for your key.
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.8-flash",
"messages": [
{ "role": "user", "content": "Explain what a REST API is 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.8-flash',
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('Key is not allowed to access this resource.');
}
if (status === 404) {
throw new Error('Model is unavailable or the name is wrong.');
}
if (status === 429 && error?.code === 'insufficient_quota') {
throw new Error('The organization has no credit or the key quota was reached.');
}
if (status === 429) {
throw new Error('Rate limit exceeded. Try again in a few seconds.');
}
throw error;
}Production usage
- use an environment variable for the key
- cap each key with a daily and monthly quota
- 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.
Last updated on