API Code Examples
Our API is designed for simplicity and is fully compatible with the OpenAI SDK. To switch your existing project to NagaAI, you only need to change the base_url
and api_key
.
- Base URL:
https://api.naga.ac/v1
- API Key: Your unique key from the NagaAI Dashboard.
View Rate Limits
You can check the specific rate limits for all models at any time by visiting: api.naga.ac/v1/limits
Client Initialization
First, set up the client in your preferred language.
- Python
- Node.js
from openai import OpenAI
client = OpenAI(
base_url='https://api.naga.ac/v1',
api_key='YOUR_API_KEY'
)
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.naga.ac/v1",
apiKey: "YOUR_API_KEY",
});
Core Endpoints
Below are practical, copy-paste examples for the most common API endpoints.
Chat Completions
Generate text-based responses for conversations or content creation.
- Python
- Node.js
- cURL
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': "What's 2+2?"}]
)
print(response.choices[0].message.content)
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "What's 2+2?" }],
});
console.log(response.choices[0].message.content);
curl https://api.naga.ac/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "What'\''s 2+2?"
}
]
}'
Image Generation
Create an image from a text prompt.
- Python
- Node.js
- cURL
response = client.images.generate(
model='flux-1-schnell',
prompt='A white siamese cat'
)
print(response.data)
const response = await client.images.generate({
model: "flux-1-schnell",
prompt: "A white siamese cat",
});
console.log(response.data);
curl https://api.naga.ac/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "flux-1-schnell",
"prompt": "A white siamese cat"
}'
Text-to-Speech (TTS)
Convert text into spoken audio.
- Python
- Node.js
- cURL
from pathlib import Path
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model='gpt-4o-mini-tts',
voice='alloy', # type: ignore
input='Hello and welcome to the NagaAI!'
)
response.stream_to_file(speech_file_path)
import fs from "fs";
import path from "path";
const speechFile = path.resolve("./speech.mp3");
const mp3 = await client.audio.speech.create({
model: "gpt-4o-mini-tts",
voice: "alloy",
input: "Hello and welcome to the NagaAI!",
});
const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile(speechFile, buffer);
curl https://api.naga.ac/v1/audio/speech \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini-tts",
"voice": "alloy",
"input": "Hello and welcome to the NagaAI!"
}' \
--output speech.mp3
Speech-to-Text (STT)
Transcribe an audio file into text.
- Python
- Node.js
- cURL
audio_file = open('/path/to/file/audio.mp3', 'rb')
transcription = client.audio.transcriptions.create(
model='gpt-4o-transcribe',
file=audio_file
)
print(transcription.text)
import fs from "fs";
const transcription = await client.audio.transcriptions.create({
file: fs.createReadStream("/path/to/file/audio.mp3"),
model: "gpt-4o-transcribe",
});
console.log(transcription.text);
curl https://api.naga.ac/v1/audio/transcriptions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F file="@/path/to/file/audio.mp3" \
-F model="gpt-4o-transcribe"
Translations
Translate an audio file into English.
- Python
- Node.js
- cURL
audio_file = open('/path/to/file/german.mp3', 'rb')
translation = client.audio.translations.create(
model='whisper-large-v3',
file=audio_file
)
print(translation.text)
import fs from "fs";
const translation = await client.audio.translations.create({
file: fs.createReadStream("/path/to/file/german.mp3"),
model: "whisper-large-v3",
});
console.log(translation.text);
curl https://api.naga.ac/v1/audio/translations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F file="@/path/to/file/german.mp3" \
-F model="whisper-large-v3"
Image Edits
Edit an existing image based on a prompt.
- Python
- Node.js
- cURL
response = client.images.edit(
model='gpt-image-1',
image=[open("image_1.png", "rb"), open("image_2.png", "rb")],
prompt='Make the clouds in image 1 look like the ones in image 2'
)
print(response.data)
import fs from "fs";
const response = await client.images.edit({
model: "gpt-image-1",
image: fs.createReadStream("image_1.png"),
prompt: "Make the clouds in image 1 look like the ones in image 2",
});
console.log(response.data);
curl https://api.naga.ac/v1/images/edits \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F image="@image_1.png" \
-F prompt="Make the clouds in image 1 look like the ones in image 2" \
-F model="gpt-image-1"
Embeddings
Generate vector embeddings for a piece of text.
- Python
- Node.js
- cURL
response = client.embeddings.create(
model="text-embedding-3-small",
input="The food was delicious!",
)
print(response)
const response = await client.embeddings.create({
model: "text-embedding-3-small",
input: "The food was delicious!",
});
console.log(response);
curl https://api.naga.ac/v1/embeddings \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": "The food was delicious!"
}'
Moderations
Check if a text string violates OpenAI's usage policies.
- Python
- Node.js
- cURL
response = client.moderations.create(model='omni-moderation-latest', input='Hello! Nice to meet you!')
print(response.results[0])
const response = await client.moderations.create({
model: "omni-moderation-latest",
input: "Hello! Nice to meet you!",
});
console.log(response.results[0]);
curl https://api.naga.ac/v1/moderations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "omni-moderation-latest",
"input": "Hello! Nice to meet you!"
}'