Skip to main content

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.

from openai import OpenAI

client = OpenAI(
base_url='https://api.naga.ac/v1',
api_key='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.

response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': "What's 2+2?"}]
)
print(response.choices[0].message.content)

Image Generation

Create an image from a text prompt.

response = client.images.generate(
model='flux-1-schnell',
prompt='A white siamese cat'
)
print(response.data)

Text-to-Speech (TTS)

Convert text into spoken audio.

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)

Speech-to-Text (STT)

Transcribe an audio file into text.

audio_file = open('/path/to/file/audio.mp3', 'rb')
transcription = client.audio.transcriptions.create(
model='gpt-4o-transcribe',
file=audio_file
)
print(transcription.text)

Translations

Translate an audio file into English.

audio_file = open('/path/to/file/german.mp3', 'rb')
translation = client.audio.translations.create(
model='whisper-large-v3',
file=audio_file
)
print(translation.text)

Image Edits

Edit an existing image based on a prompt.

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)

Embeddings

Generate vector embeddings for a piece of text.

response = client.embeddings.create(
model="text-embedding-3-small",
input="The food was delicious!",
)
print(response)

Moderations

Check if a text string violates OpenAI's usage policies.

response = client.moderations.create(model='omni-moderation-latest', input='Hello! Nice to meet you!')
print(response.results[0])