API Documentation
Authentication
All API requests require authentication using your API token in the Authorization header:
Authorization: Bearer your-token-here
API Endpoints
POST
/v1/completions
Create a completion for the provided prompt and parameters.
Request Parameters
Parameter | Type | Description |
---|---|---|
model | string | ID of the model to use (gpt-4, gpt-3.5-turbo, claude-2) |
prompt | string | The prompt to generate completions for |
max_tokens | integer | Maximum number of tokens to generate |
Rate Limits
Rate limits are based on your token balance and the models you use:
- GPT-4: 500 requests per minute
- GPT-3.5: 3,500 requests per minute
- Claude 2: 500 requests per minute
Error Handling
Status Code | Description |
---|---|
401 | Invalid authentication token |
429 | Rate limit exceeded |
402 | Insufficient token balance |
Code Examples
Python
import requests
# Your API key from TokenCheap dashboard
api_key = "your-token-here"
# Set up the headers with authentication
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Request parameters
data = {
"model": "gpt-4",
"prompt": "Hello, world!",
"max_tokens": 100
}
try:
# Make the API request
response = requests.post(
"https://api.tokencheap.com/v1/completions",
headers=headers,
json=data
)
# Check if the request was successful
response.raise_for_status()
# Print the response
result = response.json()
print(result)
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
except requests.exceptions.ConnectionError as e:
print(f"Connection Error: {e}")
except requests.exceptions.Timeout as e:
print(f"Timeout Error: {e}")
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
JavaScript
// Function to call TokenCheap API
async function getCompletion() {
// Your API key from TokenCheap dashboard
const apiKey = 'your-token-here';
try {
// Make the API request
const response = await fetch('https://api.tokencheap.com/v1/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4',
prompt: 'Hello, world!',
max_tokens: 100
})
});
// Check if the request was successful
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Parse and log the JSON response
const data = await response.json();
console.log(data);
return data;
} catch (error) {
console.error('Error:', error);
}
}
// Call the function
getCompletion();
cURL
curl -X POST 'https://api.tokencheap.com/v1/completions' \
-H 'Authorization: Bearer your-token-here' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4",
"prompt": "Hello, world!",
"max_tokens": 100
}'