AI APIs: Protocol, API Key और पहला अनुरोध
सही AI API protocol चुनें, key सुरक्षित रखें, minimal request भेजें और response, routing व usage record को verify करें।
विषय-सूची
किसी AI API को connect करने से पहले यह पहचानें कि आपका client कौन-सा contract चाहता है: OpenAI-compatible या Anthropic-compatible। फिर provider का documented Base URL इस्तेमाल करें, API Key को source code से बाहर रखें, एक छोटी request भेजें और response के साथ उसका usage record भी verify करें। Settings screen का सफलतापूर्वक save होना यह साबित नहीं करता कि request सही endpoint तक पहुँची।
अगर आपको vendor-specific web subscription के बजाय API gateway चाहिए, तो BetterToken AI API overview से शुरू करें। BetterToken अलग OpenAI-compatible और Anthropic-compatible interfaces देता है। आप अपना BetterToken account और अपनी API Key ही इस्तेमाल करते हैं; यह key OpenAI या Anthropic Console key नहीं है।
API access, web subscription और shared account
ये अलग-अलग products हैं:
| Access path | आपको क्या मिलता है | इससे क्या साबित नहीं होता |
|---|---|---|
| API access | अपनी key से authenticated HTTP requests | Vendor की consumer chat subscription का access |
| Web subscription | किसी खास product interface और उसकी included limits | Transfer की जा सकने वाली API balance या third-party API Key |
| Shared account | किसी और का login session | सुरक्षित या production के योग्य integration |
सामान्य development में वही account और key इस्तेमाल करें जिन पर आपका control हो। खरीदे हुए या shared login पर integration न बनाएँ।
1. Client के आधार पर protocol चुनें
Model चुनने से पहले client या SDK की documentation पढ़ें। Tool अगर OpenAI SDK, Chat Completions, Responses API या OPENAI_BASE_URL जैसे field की अपेक्षा करता है, तो OpenAI-compatible protocol चुनें। अगर वह Messages requests बनाता है और ANTHROPIC_BASE_URL या x-api-key चाहता है, तो Anthropic-compatible protocol चुनें।
Model का नाम protocol तय नहीं करता। Client को वही request contract बनाना आना चाहिए जिसे endpoint स्वीकार करता है।
OpenAI-compatible provider के लिए Base URL और Chat Completions path ऐसे हो सकते हैं:
Base URL: https://api.example.com/v1
Full path: https://api.example.com/v1/chat/completions
Anthropic-compatible provider के लिए Base URL https://api.example.com और full Messages path https://api.example.com/v1/messages जैसा हो सकता है। ये केवल URL forms हैं; वास्तविक values चुने गए provider की documentation से ही लें।
क्या आपको प्रोटोकॉल और पहले अनुरोध के फ़ील्ड जाँचने हैं? API कॉन्फ़िगरेशन संदर्भ खोलें
2. Base URL और request path में अंतर समझें
SDK या tool आम तौर पर Base URL माँगता है और resource path खुद जोड़ता है। Direct HTTP call के लिए पूरा path चाहिए।
OpenAI-compatible raw path: https://api.example.com/v1/chat/completions
Anthropic Messages raw path: https://api.example.com/v1/messages
जिस field में केवल Base URL अपेक्षित हो, उसमें पूरा request path paste न करें। वरना client resource को दोबारा जोड़ सकता है और 404 लौटा सकता है।
3. API Key को code से बाहर रखें
पहले local test के लिए environment variables इस्तेमाल करें, फिर production credentials को अपनी platform के secret manager में ले जाएँ।
export API_KEY="your_api_key_here"
export MODEL_ID="your_current_model_id"
export OPENAI_BASE_URL="https://api.example.com/v1"
export ANTHROPIC_BASE_URL="https://api.example.com"
Real key को source code, .env.example, prompt, issue, screenshot या support message में कभी न डालें। Marketing name से अंदाजा लगाने के बजाय provider documentation या model catalog से current exact Model ID copy करें।
4. Minimal OpenAI-compatible request भेजें
Streaming या tools enable करने से पहले छोटी text-only request इस्तेमाल करें:
curl "$OPENAI_BASE_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "'"$MODEL_ID"'",
"messages": [{"role": "user", "content": "Reply with API_OK"}],
"max_tokens": 16
}'
दूसरों के साथ share किए जाने वाले logs में curl -v न चलाएँ, क्योंकि verbose output sensitive headers दिखा सकता है।
5. Minimal Anthropic-compatible request भेजें
Messages request में authentication header और body structure अलग होता है:
curl "$ANTHROPIC_BASE_URL/v1/messages" \
-H "x-api-key: $API_KEY" \
-H "anthropic-version: CURRENT_SUPPORTED_VERSION" \
-H "Content-Type: application/json" \
-d '{
"model": "'"$MODEL_ID"'",
"max_tokens": 16,
"messages": [{"role": "user", "content": "Reply with API_OK"}]
}'
CURRENT_SUPPORTED_VERSION एक placeholder है। Test से पहले API reference में currently supported header की पुष्टि करें।
6. Response और usage record verify करें
पहला test तभी पूरा है जब ये signals एक-दूसरे से मेल खाएँ:
- HTTP status success दिखाए;
- response में expected Model ID या उसका documented display value हो;
- चुने गए contract में expected content और
usagefields मिलें; - provider के usage या billing record में expected status और charge वाली request हो।
Model availability, Model IDs और prices बदलते रहते हैं। Budget calculate करने से पहले चुने गए provider का current catalog और price page जाँचें।
7. Response layer के अनुसार troubleshooting करें
- 401: key, extra spaces और authentication method जाँचें। Bearer और
x-api-keyinterchangeable नहीं हैं। - 404: Base URL को full path से compare करें। Duplicated
/v1,/chat/completionsया/messagesखोजें। - model not found: current exact Model ID copy करें और पुष्टि करें कि वह चुने गए key group तथा protocol के लिए available है।
- Timeout या TLS error: local proxy, firewall, DNS और certificate conditions को API response से अलग जाँचें। TLS verification को स्थायी रूप से disable न करें।
व्यावहारिक क्रम यह है: client contract पहचानें, key को secret रखें, सही Base URL सेट करें, छोटी request भेजें और response तथा usage record verify करें। उसके बाद ही streaming, tools, longer context या agent workflow जोड़ें।
अगला कदम: OpenAI-compatible API
अपनी key और OpenAI-compatible route के साथ practical setup के लिए OpenAI API page देखें। यह BetterToken की compatible API बताती है, official OpenAI key नहीं।
URL forms such as https://api.example.com, https://api.example.com/v1, https://api.example.com/v1/messages, and https://api.example.com/v1/chat/completions are examples; use the selected provider’s documented values.