API Documentation

Complete guide to integrating AptRouter's prompt optimization API. Save up to 25% on token usage while maintaining quality and semantic meaning.

Quickstart

Get started with the AptRouter API in just a few steps.

1Create an Account

Sign up at aptrouter.ai/signup to get started.

2Purchase Credits

Add credits to your account. £0.10 per 1M tokens (£1 = 10M tokens). Minimum purchase £5.

3Generate an API Key

Go to the API Keys page and create a new key. Save it securely - it won't be shown again.

4Start Optimizing

Use your API key to send prompts and receive optimized versions.

Authentication

All protected endpoints require an API key. Get your API key from your dashboard.

API Key Format

text
sk_live_<base64_encoded_random_string>

Authentication Methods

Option 1: X-API-Key Header (Recommended)

bash
X-API-Key: sk_live_abc123def456...

Option 2: Authorization Bearer Token

bash
Authorization: Bearer sk_live_abc123def456...

Security Notes:

  • ⚠️ Never expose your API key in client-side code
  • ✅ Store keys in environment variables
  • 🔄 Rotate keys regularly
  • 🚫 Don't commit keys to version control
bash
curl https://api.aptrouter.ai/v1/optimize \
  -H "X-API-Key: YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your prompt text"}'

Security Warning

Never expose your API key in client-side code. Always make API calls from your backend.

API Reference

POST
/v1/optimize

Optimize a prompt to reduce token usage while maintaining semantic meaning and quality.

Request Body

json
{
  "prompt": "string",                    // Required: The prompt to optimize (1-50,000 chars)
  "similarity_threshold": 0.90,          // Optional: Minimum similarity (0.0-1.0, default: 0.90)
  "target_model": "string",              // Optional: Deprecated - ignored
  "metadata": {                          // Optional: Custom metadata
    "client_id": "string",
    "user_id": "string"
  }
}

Response (200 OK)

json
{
  "optimized_prompt": "string",          // The optimized version
  "original_prompt": "string",           // Your original prompt
  "original_tokens": 32,                 // Original token count
  "compressed_tokens": 15,               // Optimized token count
  "tokens_saved": 17,                    // Tokens saved
  "similarity_score": 0.96,              // Quality score (0.0-1.0)
  "latency_ms": 18.5,                    // Processing time
  "cache_hit": true,                     // Whether from cache
  "optimizer_model": "string"            // Model used for optimization
}

Error Responses

400
Bad Request - Invalid input
401
Unauthorized - Invalid API key
413
Payload Too Large - Exceeds token limit
429
Rate Limit Exceeded

Examples

bash
1curl -X POST https://api.aptrouter.ai/v1/optimize \
2 -H "Content-Type: application/json" \
3 -H "X-API-Key: sk_live_your_api_key_here" \
4 -d '{
5 "prompt": "Write a detailed Python function that calculates factorial using recursion",
6 "similarity_threshold": 0.90
7 }'

Error Codes

Common error responses and how to handle them.

400

Bad Request

Invalid request format or missing required fields. Check your request body.

401

Unauthorized

Missing or invalid API key. Ensure your X-API-Key header is set correctly.

402

Insufficient Credits

Your account has run out of credits. Add more credits to continue.

429

Rate Limit Exceeded

Too many requests. Current limit: 100 requests per minute.

500

Internal Server Error

Something went wrong on our end. Please try again or contact support.

Error Response Format

json
{
  "success": false,
  "error": {
    "code": "INSUFFICIENT_CREDITS",
    "message": "Your account has insufficient credits",
    "details": {
      "required": 1000,
      "available": 250
    }
  }
}

Support

Need help with your integration? We're here to assist.

Get help with technical questions, billing issues, or general inquiries.

Community Discord

Join our Discord server

Connect with other developers, share tips, and get quick answers from the community.

Best Practices

  • Implement retry logic with exponential backoff for failed requests
  • Cache optimized prompts to reduce API calls and costs
  • Monitor your credit balance regularly
  • Store API keys in environment variables, never in code
  • Review the Activity page for usage insights
AptRouter - Prompt Optimization for LLMs