Skip to main content

Step 1: Get your API Key

  1. Sign up at samurai-ai.com
  2. Go to your Dashboard
  3. Click API Keys → Create New Key
  4. Copy your key — it starts with sk-samurai-
Store your API key in an environment variable. Never commit it to version control or expose it in client-side code.

Step 2: Install the SDK

Use the official OpenAI SDK — no special Samurai package needed:
pip install openai

Step 3: Set your environment variable

export SAMURAI_API_KEY="sk-samurai-YOUR_KEY"
Or create a .env file:
.env
SAMURAI_API_KEY=sk-samurai-YOUR_KEY

Step 4: Make your first request

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("SAMURAI_API_KEY"),
    base_url="https://www.samuraiapi.in/v1"  # Only change from OpenAI
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is bushido?"}
    ]
)

print(response.choices[0].message.content)
# => "Bushido is the code of ethics followed by Japanese samurai warriors..."
You’ll get a response like:
{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Bushido is the ethical code of conduct historically observed by the samurai of Japan..."
    },
    "finish_reason": "stop"
  }],
  "usage": { "prompt_tokens": 24, "completion_tokens": 87, "total_tokens": 111 }
}

Try a cheaper model (same quality)

# DeepSeek V3 — $0.007/1M input tokens (vs $1.25 for GPT-4o)
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "What is bushido?"}]
)
print(response.choices[0].message.content)

Try streaming

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Tell me a haiku about samurai."}],
    stream=True
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

What’s next?

Chat Completions

Full parameter reference and multi-turn examples.

Streaming

Real-time streaming with SSE and Vercel AI SDK.

All Models

GPT-4.1, Claude Opus 4, Gemini 2.5 Pro, Llama 3.3 and 400+ more.

API Playground

Test every endpoint live in your browser.

Step 1: Get your API Key

  1. Sign up at samurai-ai.com
  2. Go to your Dashboard
  3. Click API KeysCreate New Key
  4. Copy your key — it starts with sk-samurai-
Never share your API key or commit it to version control. Always use environment variables.

Step 2: Install the SDK

Samurai AI is 100% OpenAI-compatible. Use the official OpenAI SDK — no special package needed:
pip install openai

Step 3: Make your first request

from openai import OpenAI

client = OpenAI(
    api_key="sk-samurai-YOUR_KEY",
    base_url="https://api.samuraiapi.in/v1"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello! What can you do?"}]
)

print(response.choices[0].message.content)

What’s next?

Chat Completions

Learn all parameters and options for chat.

Browse Models

See all 400+ available models.

Image Generation

Generate images with DALL-E 3, Flux, and more.

Streaming

Stream responses in real time.