मध्यम

API के through AI integrate करना: एक practical guide

एक API, 367 models। OpenAI-compatible — base URL change करें, अपना code रखें। एक hour से कम में zero से working AI integration तक पहुँचने के लिए जो चाहिए वो यहाँ है।
Sarah Chen March 2026 10 min read

आपके पास एक app है। आप चाहते हैं कि यह AI से बात करे। शायद आप chat completions चाहते हैं, शायद image generation, शायद दोनों। Good news: अगर आपने OpenAI के API use किया है, तो आप पहले से जानते हैं कि हमारा कैसे use करें। Better news: आपको एक single endpoint के through 61 providers से 367 models तक access मिलता है।

कोई नया SDK सीखने की जरूरत नहीं। कोई proprietary protocol नहीं। बस base URL और अपनी API key change करें।

Step 1: अपनी API key पाएँ

zubnet.com पर sign up करें और अपनी workspace settings पर navigate करें। आपकी API key वहाँ रहती है। एक recognizable prefix से शुरू होती है:

zub_live_a1b2c3d4e5f6...

इसे secret रखें। Password की तरह treat करें। अगर leak हो, तो तुरंत अपनी workspace settings से rotate करें — जैसे ही आप नई generate करते हैं old key काम करना बंद कर देती है।

Step 2: अपनी base URL set करें

हर request जाती है:

https://api.zubnet.ai/v1

बस। /v1 prefix OpenAI की convention से match करता है, तो कोई भी library, tool या framework जो OpenAI को support करता है बस URL swap करके काम करेगा।

Step 3: अपनी पहली request करें

curl use करके

संभव सबसे simple test — एक message के साथ एक chat completion:

curl https://api.zubnet.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer zub_live_YOUR_KEY" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'

आपको choices[0].message.content में model की reply के साथ एक JSON response मिलेगा। Exactly same shape OpenAI's response के रूप में।

OpenAI SDK के साथ Python use करके

अगर आपके पास नहीं है तो SDK install करें:

pip install openai

फिर हमारी base URL के साथ use करें:

from openai import OpenAI

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

response = client.chat.completions.create(
    model="claude-sonnet-4-20250514",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing in three sentences."}
    ],
    max_tokens=500,
    temperature=0.7
)

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

यह एक complete, working example है। Copy करें, paste करें, API key replace करें, run करें।

Model parameter

model field वो है जहाँ यह interesting होता है। एक provider तक lock होने के बजाय, आप 61 providers में 367 models में से choose करते हैं। कुछ examples:

Popular model IDs:

claude-sonnet-4-20250514Anthropic Claude Sonnet 4
gpt-4.1OpenAI GPT-4.1
gemini-2.5-pro-preview-05-06 — Google Gemini 2.5 Pro
deepseek-r1 — DeepSeek R1 reasoning model
flux-1.1-proBlack Forest Labs FLUX for images
kling-v2.5-master — Kling video generation

Full list हमारे models page पर है, type (LLM, image, video, audio, 3D, code) और provider द्वारा filterable। जो model ID आप वहाँ देखते हैं वही exactly model field में जाता है।

Image generation

Image models same endpoint pattern use करते हैं। यहाँ Python के through FLUX है:

response = client.images.generate(
    model="flux-1.1-pro",
    prompt="A serene Japanese garden at dawn, soft morning light",
    n=1,
    size="1024x1024"
)

image_url = response.data[0].url
print(image_url)

Response में हमारे CDN पर hosted generated image के लिए एक URL include है। Download करें, display करें, embed करें — URL 24 hours के लिए valid है।

Streaming responses

Chat models के लिए, streaming tokens को generate होते ही send करता है instead of full response के लिए wait करने के। यह किसी भी user-facing chat interface के लिए essential है — कोई 10 seconds तक blank screen नहीं देखना चाहता।

stream = client.chat.completions.create(
    model="claude-sonnet-4-20250514",
    messages=[
        {"role": "user", "content": "Write a short poem about code."}
    ],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Under the hood, यह Server-Sent Events (SSE) use करता है। हर chunk HTTP response में एक data: line के रूप में आता है। OpenAI SDK parsing आपके लिए handle करता है, लेकिन अगर आप raw HTTP use करते हैं, तो आप ऐसी lines देखेंगे:

data: {"choices":[{"delta":{"content":"Once"},"index":0}]}
data: {"choices":[{"delta":{"content":" upon"},"index":0}]}
data: {"choices":[{"delta":{"content":" a"},"index":0}]}
...
data: [DONE]

Stream data: [DONE] के साथ end होता है। हर line parse करें, delta.content extract करें, output buffer में append करें। वो पूरा protocol है।

Error handling

API standard HTTP status codes use करता है। यहाँ वो हैं जो आप actually encounter करेंगे:

400 Bad Request — आपकी request body malformed है। JSON structure check करें, make sure messages एक array है, make sure model एक valid model ID है।

401 Unauthorized — आपकी API key missing, invalid या expired है। Authorization: Bearer header check करें।

403 Forbidden — आपके account को इस model या feature तक access नहीं है। अपनी workspace subscription tier check करें।

429 Too Many Requests — आपने rate limit hit किया। Response में एक Retry-After header include है जो बताता है कितने seconds wait करना है। Exponential backoff implement करें:

import time
from openai import RateLimitError

max_retries = 3
for attempt in range(max_retries):
    try:
        response = client.chat.completions.create(
            model="claude-sonnet-4-20250514",
            messages=[{"role": "user", "content": "Hello"}]
        )
        break
    except RateLimitError:
        wait = 2 ** attempt
        print(f"Rate limited. Waiting {wait}s...")
        time.sleep(wait)

500 / 502 / 503 — server-side problems। ये rare हैं लेकिन होते हैं। Backoff के साथ retry करें। अगर persist करें, तो हमारी Pulse status page check करें।

Authentication details

हर request को एक Authorization header चाहिए:

Authorization: Bearer zub_live_YOUR_KEY

Key आपके workspace पर scoped है। एक ही workspace में सभी team members same key और usage pool share करते हैं। अगर आपको per-team-member separate tracking चाहिए, तो separate workspaces create करें।

Security best practices:

• कभी source code में keys hardcode न करें — environment variables use करें
• कभी git में keys commit न करें — .gitignore में .env files use करें
• Workspace settings से keys regularly rotate करें
• Development और production के लिए separate keys use करें

Rate limits

Rate limits आपके plan tier पर depend करते हैं। Defaults ज्यादातर use cases के लिए generous हैं:

Requests per minute: plan द्वारा varies (60-600 RPM)
Tokens per minute: model और plan द्वारा varies
Concurrent requests: कोई hard limit नहीं, लेकिन अगर आप बहुत aggressively burst करते हैं तो 429s fire होंगे

Rate limit headers हर response में included हैं:

x-ratelimit-limit-requests: 120
x-ratelimit-remaining-requests: 119
x-ratelimit-reset-requests: 0.5s

ये headers पढ़ें। अपना client उनका respect करने के लिए build करें। आपके users thank करेंगे जब उनकी requests randomly fail नहीं होंगी।

सब कुछ एक साथ

यहाँ एक production-ready Python function है जो errors, retries और streaming handle करती है:

import os
from openai import OpenAI, APIError, RateLimitError
import time

client = OpenAI(
    api_key=os.environ["ZUBNET_API_KEY"],
    base_url="https://api.zubnet.ai/v1"
)

def ask(prompt, model="claude-sonnet-4-20250514", stream=False):
    for attempt in range(3):
        try:
            response = client.chat.completions.create(
                model=model,
                messages=[{"role": "user", "content": prompt}],
                stream=stream
            )
            if stream:
                return response  # iterate over chunks
            return response.choices[0].message.content
        except RateLimitError:
            time.sleep(2 ** attempt)
        except APIError as e:
            if attempt == 2:
                raise
            time.sleep(1)

# Usage
print(ask("Summarize the latest in quantum computing"))
print(ask("Same question, different model", model="gpt-4.1"))

एक string change करके models switch करें। कोई code refactoring नहीं, कोई new dependencies नहीं, कोई different response format नहीं। यही OpenAI-compatibility का पूरा point है।

Key insight: OpenAI-compatibility सिर्फ convenience नहीं है। इसका मतलब है ecosystem में हर tool — LangChain, LlamaIndex, AutoGen, Cursor, Continue, कोई भी tool जो OpenAI protocol speak करता है — Zubnet के साथ out-of-the-box काम करता है। Base URL change करें, अपनी key plug करें, और आपके पास किसी भी framework में 367 models हैं।

इस guide के सभी code examples मार्च 2026 के अनुसार tested और working हैं। Copy करें, run करें, ऊपर build करें। वो इसी के लिए हैं।

शुरू करने को ready? zubnet.com पर अपनी API key पाएँ

Sarah Chen
Zubnet · March 2026
ESC