Loading...
Loading...
Direct API integration with clean subdomain URLs and Bearer token authentication.
All API endpoints are now available at api.costlens.dev
with cleaner URLs and Bearer token authentication.
Get your API key from Settings → API Keys in your dashboard.
Quick Test:
curl -X POST https://api.costlens.dev/cost-analysis \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello", "model": "gpt-4", "provider": "openai"}'
https://api.costlens.dev
All API requests require authentication via API key in the Authorization header:
Authorization: Bearer your_api_key_here
Analyze AI costs and get optimization recommendations.
POST https://api.costlens.dev/cost-analysis
Content-Type: application/json
Authorization: Bearer your_api_key_here
{
"prompt": "Your AI prompt here",
"model": "gpt-4",
"provider": "openai"
}
{
"prompt": "Your AI prompt here",
"model": "gpt-4",
"provider": "openai",
"estimatedTokens": 25,
"estimatedCost": 0.00075,
"recommendations": [
"Consider using GPT-3.5-turbo for 90% cost savings on simple tasks",
"Use prompt caching to reduce repeated costs",
"Consider batch processing for multiple requests"
],
"timestamp": "2025-01-15T10:30:00.000Z"
}
Track an AI API call.
POST https://api.costlens.dev/integrations/run
Content-Type: application/json
Authorization: Bearer your_api_key_here
{
"provider": "openai",
"model": "gpt-4",
"input": "{\"messages\":[...]}",
"output": "Hello! How can I help you?",
"tokensUsed": 150,
"latency": 1234,
"success": true,
"promptId": "greeting-v1"
}
{
"success": true,
"runId": "run_abc123"
}
Get analytics overview.
GET https://api.costlens.dev/analytics/overview?startDate=2025-01-01&endDate=2025-01-31
startDate
(ISO 8601) - Start date for analyticsendDate
(ISO 8601) - End date for analytics{
"totalRuns": 1234,
"totalCost": 45.67,
"totalTokens": 123456,
"avgCostPerRun": 0.037,
"successRate": 98.5,
"avgLatency": 1234
}
Get current usage stats.
GET https://api.costlens.dev/usage
{
"plan": "free",
"runsThisMonth": 234,
"runsLimit": 1000,
"percentUsed": 23.4
}
Rate limits are per API key:
Plan | Requests/Minute |
---|---|
Free | 10 |
Starter | 60 |
Pro | 300 |
Enterprise | 1,000 |
When rate limited, you'll receive:
{
"error": "Rate limit exceeded",
"retryAfter": 60
}
Code | Meaning |
---|---|
401 | Unauthorized - Invalid API key |
402 | Payment Required - Upgrade needed |
429 | Too Many Requests - Rate limit exceeded |
500 | Internal Server Error |
# Cost Analysis
curl -X POST https://api.costlens.dev/cost-analysis \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain quantum computing",
"model": "gpt-4",
"provider": "openai"
}'
# Track AI Call
curl -X POST https://api.costlens.dev/integrations/run \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"provider": "openai",
"model": "gpt-4",
"input": "{\"messages\":[...]}",
"output": "Hello!",
"tokensUsed": 150,
"latency": 1234,
"success": true
}'
import requests
# Cost Analysis
response = requests.post(
'https://api.costlens.dev/cost-analysis',
headers={
'Authorization': 'Bearer your_api_key_here',
'Content-Type': 'application/json'
},
json={
'prompt': 'Explain quantum computing',
'model': 'gpt-4',
'provider': 'openai'
}
)
# Track AI Call
response = requests.post(
'https://api.costlens.dev/integrations/run',
headers={
'Authorization': 'Bearer your_api_key_here',
'Content-Type': 'application/json'
},
json={
'provider': 'openai',
'model': 'gpt-4',
'input': '{"messages":[...]}',
'output': 'Hello!',
'tokensUsed': 150,
'latency': 1234,
'success': True
}
)