AI API Integration Guide
AI API Integration Guide for Developers
Integrating AI APIs into your application is one of the most impactful things you can do as a developer in 2026. Whether you are building a chatbot, adding intelligent features to an existing product, or creating AI-powered developer tools, this guide walks you through the complete integration process — from your first API call to a production-ready implementation.
Getting Started: Your First API Call
The fastest way to get started is using the OpenAI-compatible Chat Completions format, which is supported by virtually every AI provider either natively or through compatibility layers.
Python
from openai import OpenAI
# Connect through a relay service for multi-model access
client = OpenAI(
base_url="https://claude4u.com/v1",
api_key="your-api-key"
)
response = client.chat.completions.create(
model="claude-sonnet-4-20250514",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain REST APIs in one paragraph."}
]
)
print(response.choices[0].message.content)
Node.js / JavaScript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://claude4u.com/v1',
apiKey: 'your-api-key'
});
const response = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain REST APIs in one paragraph.' }
]
});
console.log(response.choices[0].message.content);
cURL
curl https://claude4u.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain REST APIs in one paragraph."}
]
}'
Understanding the Message Format
The Chat Completions API uses a messages array with three role types:
- system: Sets the AI's behavior, personality, and constraints. Sent once at the beginning.
- user: Messages from the end user or your application.
- assistant: Previous AI responses, included for conversation continuity.
Implementing Streaming
Streaming is essential for interactive applications. Instead of waiting for the complete response, you receive tokens as they are generated:
// Node.js streaming example
const stream = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
messages: [{ role: 'user', content: 'Write a short poem.' }],
stream: true
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}
// Python streaming example
stream = client.chat.completions.create(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Write a short poem."}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
Building a Multi-Turn Conversation
For chatbot or conversational applications, maintain the conversation history and send it with each request:
class Conversation {
constructor(systemPrompt) {
this.messages = [
{ role: 'system', content: systemPrompt }
];
}
async send(userMessage) {
this.messages.push({ role: 'user', content: userMessage });
const response = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
messages: this.messages
});
const assistantMessage = response.choices[0].message.content;
this.messages.push({ role: 'assistant', content: assistantMessage });
return assistantMessage;
}
}
// Usage
const chat = new Conversation('You are a Python tutor.');
await chat.send('How do list comprehensions work?');
await chat.send('Can you show me a more complex example?');
Error Handling for Production
A production-grade integration must handle various failure modes:
async function robustAPICall(messages, options = {}) {
const { maxRetries = 3, timeout = 30000 } = options;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeout);
const response = await client.chat.completions.create(
{
model: options.model || 'claude-sonnet-4-20250514',
messages,
...options
},
{ signal: controller.signal }
);
clearTimeout(timer);
return response;
} catch (error) {
if (error.status === 429 || error.status === 529) {
const delay = Math.pow(2, attempt) * 1000;
await new Promise((r) => setTimeout(r, delay));
continue;
}
if (error.name === 'AbortError') {
throw new Error('Request timed out');
}
throw error;
}
}
throw new Error('Max retries exceeded');
}
Working with Function Calling / Tool Use
Function calling allows the AI to invoke your application's functions based on the conversation context:
const response = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current weather for a city',
parameters: {
type: 'object',
properties: {
city: { type: 'string', description: 'City name' }
},
required: ['city']
}
}
}
]
});
Choosing Your API Access Method
You have several options for accessing AI APIs:
- Direct provider access: Sign up with each provider individually. Full control but more complexity.
- Cloud platform: AWS Bedrock or Azure OpenAI for enterprise environments with existing cloud infrastructure.
- Relay service: Services like claude4u.com provide unified access to multiple providers through a single API endpoint and key. Best for teams that want simplicity and multi-model flexibility.
Start with the simplest approach that meets your needs. A relay service is often the fastest path from zero to a working integration, and you can always migrate to direct access later if your requirements change.
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI