OpenAI API
Overview
The OpenAI API provides access to powerful AI models that can understand and generate natural language, code, and images. The API can be applied to virtually any task that involves understanding or generating natural language, code, or images.
Features
- Text generation and completion with GPT models
- Chat-based interactions using chat models
- Image generation with DALL-E
- Fine-tuning models on your specific use case
- Embeddings for advanced text analysis and search
- Moderation API to detect harmful content
Authentication
Authentication to the OpenAI API is done by providing your API key in the request header:
Authorization: Bearer YOUR_API_KEY
API keys can be managed in the OpenAI dashboard.
Rate Limits
Rate limits vary based on your usage tier and the specific model being used. The API returns a 429 status code when rate limits are exceeded, along with information about when you can retry the request.
Examples
// Generate text with GPT
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'You are a helpful assistant.'
},
{
role: 'user',
content: 'Explain quantum computing in simple terms.'
}
],
temperature: 0.7
})
})
.then(response => response.json())
.then(data => console.log(data.choices[0].message.content));
// Generate an image
fetch('https://api.openai.com/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'A futuristic city with flying cars',
n: 1,
size: '1024x1024'
})
})
.then(response => response.json())
.then(data => console.log(data.data[0].url));
Documentation
For complete documentation, visit the OpenAI API Documentation.
Endpoints
Contribute to this API
Know something more about this API? Help improve this page by editing it on GitHub .