AgnicPay

SDK Examples

Use the AI Gateway with popular SDKs and languages

SDK Examples

The AI Gateway is fully compatible with the OpenAI SDK. Use your existing code with just a base URL change.

Base URL

https://api.agnic.ai/v1

Python

Installation

pip install openai

Basic Usage

from openai import OpenAI
 
client = OpenAI(
    api_key="agnic_tok_YOUR_TOKEN",
    base_url="https://api.agnic.ai/v1"
)
 
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
 
print(response.choices[0].message.content)

Async Client

from openai import AsyncOpenAI
import asyncio
 
client = AsyncOpenAI(
    api_key="agnic_tok_YOUR_TOKEN",
    base_url="https://api.agnic.ai/v1"
)
 
async def main():
    response = await client.chat.completions.create(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)
 
asyncio.run(main())

With Environment Variables

export OPENAI_API_KEY="agnic_tok_YOUR_TOKEN"
export OPENAI_BASE_URL="https://api.agnic.ai/v1"
from openai import OpenAI
 
client = OpenAI()  # Reads from env vars
 
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

Error Handling

from openai import OpenAI, APIError, RateLimitError, AuthenticationError
 
client = OpenAI(
    api_key="agnic_tok_YOUR_TOKEN",
    base_url="https://api.agnic.ai/v1"
)
 
try:
    response = client.chat.completions.create(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)
except AuthenticationError:
    print("Invalid API token")
except RateLimitError:
    print("Rate limit exceeded - try again later")
except APIError as e:
    if e.status_code == 402:
        print("Payment required - check balance or limits")
    else:
        print(f"API error: {e}")

JavaScript / TypeScript

Installation

npm install openai

Basic Usage

import OpenAI from 'openai';
 
const client = new OpenAI({
  apiKey: 'agnic_tok_YOUR_TOKEN',
  baseURL: 'https://api.agnic.ai/v1'
});
 
const response = await client.chat.completions.create({
  model: 'openai/gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
 
console.log(response.choices[0].message.content);

CommonJS

const OpenAI = require('openai');
 
const client = new OpenAI({
  apiKey: 'agnic_tok_YOUR_TOKEN',
  baseURL: 'https://api.agnic.ai/v1'
});
 
async function main() {
  const response = await client.chat.completions.create({
    model: 'openai/gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  console.log(response.choices[0].message.content);
}
 
main();

Error Handling

import OpenAI from 'openai';
 
const client = new OpenAI({
  apiKey: 'agnic_tok_YOUR_TOKEN',
  baseURL: 'https://api.agnic.ai/v1'
});
 
try {
  const response = await client.chat.completions.create({
    model: 'openai/gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  console.log(response.choices[0].message.content);
} catch (error) {
  if (error instanceof OpenAI.APIError) {
    if (error.status === 401) {
      console.log('Invalid API token');
    } else if (error.status === 402) {
      console.log('Payment required - check balance or limits');
    } else if (error.status === 429) {
      console.log('Rate limit exceeded');
    } else {
      console.log(`API error: ${error.message}`);
    }
  }
}

cURL

Basic Request

curl https://api.agnic.ai/v1/chat/completions \
  -H "Authorization: Bearer agnic_tok_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

With System Prompt

curl https://api.agnic.ai/v1/chat/completions \
  -H "Authorization: Bearer agnic_tok_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ]
  }'

Streaming

curl https://api.agnic.ai/v1/chat/completions \
  -H "Authorization: Bearer agnic_tok_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [{"role": "user", "content": "Write a poem"}],
    "stream": true
  }'

Alternative Authentication

# Using X-Agnic-Token header
curl https://api.agnic.ai/v1/chat/completions \
  -H "X-Agnic-Token: agnic_tok_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Go

package main
 
import (
    "context"
    "fmt"
    openai "github.com/sashabaranov/go-openai"
)
 
func main() {
    config := openai.DefaultConfig("agnic_tok_YOUR_TOKEN")
    config.BaseURL = "https://api.agnic.ai/v1"
 
    client := openai.NewClientWithConfig(config)
 
    resp, err := client.CreateChatCompletion(
        context.Background(),
        openai.ChatCompletionRequest{
            Model: "openai/gpt-4o",
            Messages: []openai.ChatCompletionMessage{
                {
                    Role:    openai.ChatMessageRoleUser,
                    Content: "Hello!",
                },
            },
        },
    )
 
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
 
    fmt.Println(resp.Choices[0].Message.Content)
}

Ruby

require 'openai'
 
client = OpenAI::Client.new(
  access_token: 'agnic_tok_YOUR_TOKEN',
  uri_base: 'https://api.agnic.ai/v1'
)
 
response = client.chat(
  parameters: {
    model: 'openai/gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }]
  }
)
 
puts response.dig('choices', 0, 'message', 'content')

PHP

<?php
require 'vendor/autoload.php';
 
$client = OpenAI::factory()
    ->withApiKey('agnic_tok_YOUR_TOKEN')
    ->withBaseUri('https://api.agnic.ai/v1')
    ->make();
 
$response = $client->chat()->create([
    'model' => 'openai/gpt-4o',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!']
    ]
]);
 
echo $response->choices[0]->message->content;

Authentication Methods

All SDKs support multiple authentication approaches:

Method 1: Direct Token

client = OpenAI(
    api_key="agnic_tok_YOUR_TOKEN",
    base_url="https://api.agnic.ai/v1"
)

Method 2: OAuth2 Token

client = OpenAI(
    api_key="agnic_at_YOUR_OAUTH_TOKEN",
    base_url="https://api.agnic.ai/v1"
)

Method 3: Environment Variables

export OPENAI_API_KEY="agnic_tok_YOUR_TOKEN"
export OPENAI_BASE_URL="https://api.agnic.ai/v1"

Next Steps

On this page