Claude API Anthropic ka official interface hai jiske through aap Claude AI ko apni applications, websites, aur automations mein integrate kar sakte ho. Yeh guide tumhe zero se pehli working API call tak le jaayegi - no prior API experience required. Python aur JavaScript dono examples included hain.
Claude API Kya Hai?
Claude API Anthropic ka official interface hai jiske through Claude AI ko apni website, app, ya automation mein integrate kiya ja sakta hai. Claude.ai sirf browser chat ke liye hai - product mein Claude use karne ke liye API hi chahiye hoga.
Claude.ai ek web interface hai - browser mein kholo aur chat karo. Lekin agar tum Claude ko apne product mein use karna chahte ho - apni website, app, chatbot, ya automation tool mein - to tumhe Claude API chahiye.
API ek "door" hai jo tumhare code ko Claude ke saath communicate karne deta hai. Tum ek request bhejte ho (jaise ek message), Claude process karta hai, aur response wapas aata hai - programmatically, real time mein.
| Feature | Claude.ai (Web) | Claude API |
|---|---|---|
| Usage | Browser mein manually | Code se programmatically |
| Automation | ❌ Manual only | ✅ Fully automated |
| Custom Integration | ❌ Not possible | ✅ Any app/platform |
| Pricing | Monthly subscription | Pay per token (usage-based) |
| Best For | Personal use | Building products |
API Key Setup Karo
Anthropic Console Account Banao
console.anthropic.com par jaao. Email se signup karo. New accounts ko free API credits milte hain testing ke liye.
API Key Generate Karo
Console mein "API Keys" section mein jaao. "Create Key" click karo. Key ko copy karo - yeh ek baar hi dikhti hai. Safely save karo.
Key Ko Environment Variable Mein Store Karo
Key ko apne code mein directly paste mat karo. Environment variable use karo:.env file mein: ANTHROPIC_API_KEY=sk-ant-...
⚠️ Security Warning: API key ko kabhi GitHub ya public code mein push mat karo. Agar accidental exposure ho, turant Console mein jaake key revoke karo aur naya banao.
Python Integration - First API Call
Pehle Anthropic Python SDK install karo:
TerminalAb pehli API call karo:
PythonSystem prompt ke saath (Claude ko custom role dene ke liye):
Python - With System PromptMulti-turn conversation (chat history ke saath):
Python - Multi-turn ChatJavaScript Integration
TerminalREST API Example (curl)
Agar tum kisi language mein SDK use nahi karna chahte, ya sirf quickly test karna chahte ho, to direct REST API call bhi kar sakte ho curl se. Yeh useful hai debugging ke liye ya kisi bhi language se integration ke liye jahan official SDK available nahi hai.
Response ek JSON object hoga jisme content array ke andar Claude ka reply milega. Har programming language jo HTTP requests bhej sakti hai (PHP, Ruby, Java, Go) usse tum yeh same pattern follow karke Claude integrate kar sakte ho, chahe official SDK ho ya na ho.
Key Concepts - Samajhna Zaroori Hai
Models
Anthropic ke paas teen main models hain Claude 4 family mein:
| Model | Speed | Cost | Best For |
|---|---|---|---|
| claude-haiku-4-5 | ⚡ Fastest | 💚 Cheapest | Simple tasks, high volume |
| claude-sonnet-4-6 | 🏃 Fast | 💛 Mid-range | Most production use cases |
| claude-opus-4-6 | 🧠 Slower | 🔴 Premium | Complex reasoning, research |
Tokens
Claude text ko "tokens" mein process karta hai - roughly 1 token ≈ ¾ of an English word. API charges input tokens + output tokens ke liye. max_tokens parameter se aap control kar sakte ho kitna long response aaye.
System Prompt
System prompt ek hidden instruction hai jo Claude ka role define karta hai. Yeh user ko nahi dikhti - sirf Claude ko instructions deta hai. Iska proper use production-grade AI apps ki foundation hai.
Temperature
Temperature (0-1) creativity control karta hai. 0 = deterministic, consistent responses (best for factual tasks). 1 = more creative, varied responses (best for creative writing). Default 1 hai.
Streaming Responses
Long responses ke liye, user ko poora response aane tak wait nahi karna chahiye - streaming se response word-by-word ya chunk-by-chunk aata hai, jaise Claude.ai ke chat interface mein hota hai. Python SDK mein yeh simple hai:
Python - StreamingYeh chatbots aur customer-facing tools ke liye zaroori hai - user ko lagta hai response turant aa raha hai, wait karne ka feel nahi hota, jabki backend mein poora response generate hone mein utna hi time lag raha hota hai.
Best Practices for Production
✅ Error Handling: API calls fail ho sakti hain. Always try-catch use karo aur retry logic implement karo for rate limit errors.
✅ Rate Limits: Anthropic har account ke liye rate limits set karta hai. High-volume apps ke liye batching implement karo aur delays add karo.
✅ Cost Monitoring: Console mein usage dashboard monitor karo. Unexpected costs avoid karne ke liye max_tokens reasonable set karo aur unnecessary API calls reduce karo.
✅ Streaming: Long responses ke liye streaming use karo - user ko type karte hue response dikh sake, wait nahi karna padega complete response ke liye.
Common Errors and How to Fix Them
Beginners ko API integrate karte waqt kuch errors baar-baar milte hain. Yahan sabse common issues aur unke fixes hain:
| Error | Kya Matlab Hai | Fix |
|---|---|---|
| 401 Unauthorized | API key galat hai ya missing hai | Environment variable check karo, key Console mein verify karo |
| 429 Rate Limit | Bahut zyada requests ek saath | Exponential backoff ke saath retry karo, request rate kam karo |
| 400 Invalid Request | Request body mein galat format ya missing field | model, max_tokens, messages fields double-check karo |
| overloaded_error | Anthropic ke servers temporarily busy hain | Kuch second wait karke retry karo, yeh Claude ki taraf se issue hai |
| Timeout | Response aane mein bahut time lag raha | Streaming use karo ya max_tokens kam karo long responses ke liye |
💡 Tip: Production apps mein har API call ko try-catch mein wrap karo aur specific error codes ke hisaab se alag handling karo - 429 par retry, 401 par alert bhejo team ko, aur 400 par request payload log karo debugging ke liye.
Using Claude API for Business Automation
Claude API sirf chatbots ke liye nahi hai - real business use case yeh hai ki tum WhatsApp enquiries, CRM leads, aur customer support ko automatically handle kar sako, bina har message manually reply kiye. Ek Claude-powered agent WhatsApp message padh sakta hai, customer ka intent samajh sakta hai, aur ya to instant reply de sakta hai ya sahi team member ko route kar sakta hai.
Hum apne clients ke liye exactly yeh workflows banate hain - Claude API ko WhatsApp Business API aur CRM ke saath connect karke, taaki koi bhi enquiry miss na ho aur response time minutes mein ho, hours mein nahi. Agar tum apne business mein Claude API ko is tarah use karna chahte ho, humari AI agent automation services aur WhatsApp CRM automation pages dekho - wahan poora setup process cover kiya gaya hai.
Is tarah ke automation ka biggest fayda yeh hai ki tumhara team sirf un enquiries par focus karta hai jo genuinely human attention chahti hain, jabki routine questions - pricing, availability, order status - Claude khud handle kar leta hai, consistently aur bina thakke, 24 ghante.
