Skip to main content
NinjaChat API keys are server-side credentials. Load NINJACHAT_API_KEY from your deployment platform or secret manager; never include an nj_sk_... key in browser or mobile code.

Choose an integration

TypeScript SDK

Typed clients for Node.js, Next.js and workers.

Python SDK

For Python services, notebooks and background workers.

Vercel AI SDK

Official provider for the AI SDK — generateText, streamText, tools, and structured outputs, with the routing trace on providerMetadata.

OpenAI client

Keep an existing OpenAI integration and change the base URL, API key, and model ID.

Raw REST

Call the API without an SDK.

Install

The TypeScript package requires Node.js 18 or newer. The Python package requires Python 3.10 or newer.

Create a client

You do not need to configure a base URL. Both clients default to https://www.ninjachat.ai/api/v1.
app/api/generate/route.ts
app.py

Generate a response

Use Responses for new stateless text, vision, tool, and structured-output integrations:

Use Chat Completions

Use Chat Completions when your application already works with role-based messages:
Already using OpenAI’s client? You can keep it and change the base URL and key.

Stream output

See Streaming for event behavior and complete examples.

Images and video

Video polling stops automatically when the job completes or fails. Failed generation jobs are refunded by the API.

Models, usage, and traces

See Observability for the fields to log, health checks, alerting, and signed webhook verification.

Search and webhooks

Run AI-assisted search through the same client:
For long-running video and pipeline jobs and spend alerts, register a signed webhook instead of polling — in Developers → Webhooks or via the SDK. The events are video.completed, video.failed, pipeline.completed, pipeline.failed, budget.alert, and balance.low; omit events to subscribe to all six. Store the returned signing secret immediately; it is shown once.

Handle errors

Every failed request raises a typed NinjaChatError with an HTTP status, machine-readable code, and request ID when the server produced one.
The clients retry 429, 500, 502, 503, and 504 responses (up to maxRetries, default 2), honor Retry-After, and attach an Idempotency-Key to billed requests before retrying so a retry can only replay, never double-bill. A 409 request_in_flight is retried only when the request carried an idempotency key; 408 is never retried. Pass maxRetries: 0 (max_retries=0) to disable retries. See Error handling and Rate limits. Before customer traffic, complete the go-live checklist.

Vercel AI SDK

If your app is built on the AI SDK, use the official provider rather than a generic OpenAI-compatible shim — you keep NinjaChat’s routing policy and its routing trace.
streamText, tool calling, structured outputs, and image inputs all work. Reads NINJACHAT_API_KEY from the environment, or call createNinjaChat({ apiKey }). For an ordered fallback chain, ninjachat.chain(["claude-opus-5", "gpt-5.6-sol"]) sends the models array. Media generation, search, batch, compare, and pipelines live in @ninjachat/sdk — the AI SDK provider covers text.

TypeScript on npm

@ninjachat/sdk

Python on PyPI

ninjachat

AI SDK provider on npm

@ninjachat/ai-sdk-provider

Source code

Browse the SDKs and release history

API quickstart

Make your first request

More capabilities

  • Speech: client.audio.speech.create returns audio bytes (Python) or a binary Response (TypeScript).
  • Embeddings: client.embeddings.create returns vectors.
  • Reranking: client.rerank.create returns ranked documents.
  • Anthropic Messages: client.messages.create supports messages and streaming.
  • Account management: use a management credential for keys, projects and account operations.