📚 API Documentation

Complete guide to integrating EcoAI's green optimization into your applications

📋 Table of Contents

🚀 Quick Start

Get up and running with EcoAI in under 5 minutes

1. Install the SDK

pip install ecoai

2. Get your API Key

Sign up at ecoai.club to get your free API key.

3. Optimize your first prompt

from ecoai import EcoAI

# Initialize the SDK
eco = EcoAI(api_key="your_api_key_here")

# Optimize a prompt
result = eco.optimize_prompt("Please kindly write a comprehensive analysis of the data")

print(f"Original: {result['original']}")
print(f"Optimized: {result['optimized']}")
print(f"Tokens saved: {result['tokens_saved']}")
print(f"CO2 saved: {result['carbon_savings']['co2_g_saved']:.4f}g")

🔐 Authentication

All API requests require authentication using your API key

API Key

Include your API key in the request headers:

X-API-Key: ecoai_8727f8d76454fff1aa3b786d6e980fc2

🔌 API Endpoints

Complete reference for all EcoAI API endpoints

POST /api/optimize/prompt

Optimize a prompt to reduce tokens while maintaining quality

Request Body

Parameter Type Required Description
prompt string Yes The prompt to optimize
strategy string No Optimization strategy: "balanced" (default), "aggressive", "conservative"
quality_threshold float No Minimum quality score (0.0-1.0, default: 0.95)

Example Request

{
  "prompt": "Please kindly write a comprehensive analysis of the data",
  "strategy": "balanced",
  "quality_threshold": 0.95
}

Response

200 OK Success

{
  "success": true,
  "data": {
    "original": "Please kindly write a comprehensive analysis of the data",
    "optimized": "Write a comprehensive analysis of the data",
    "tokens_before": 12,
    "tokens_after": 8,
    "tokens_saved": 4,
    "quality_score": 0.96,
    "carbon_savings": {
      "co2_g_saved": 0.0001,
      "kwh_saved": 0.000003
    },
    "strategy_used": "balanced"
  }
}
POST /api/optimize/batch

Optimize multiple prompts in a single request

Request Body

{
  "prompts": [
    "Please kindly write a comprehensive analysis",
    "Can you help me understand this concept?",
    "I would really appreciate your assistance"
  ],
  "strategy": "balanced"
}

Response

200 OK Success

{
  "success": true,
  "data": {
    "results": [
      {
        "original": "Please kindly write a comprehensive analysis",
        "optimized": "Write a comprehensive analysis",
        "tokens_saved": 2
      }
    ],
    "total_tokens_saved": 6,
    "total_co2_saved": 0.0002
  }
}
GET /api/analytics/summary

Get your optimization analytics and impact metrics

Query Parameters

Parameter Type Required Description
start_date string No Start date (YYYY-MM-DD)
end_date string No End date (YYYY-MM-DD)

Response

200 OK Success

{
  "success": true,
  "data": {
    "total_requests": 1250,
    "total_tokens_saved": 15420,
    "total_co2_saved": 0.54,
    "average_quality_score": 0.96,
    "cost_savings": 12.50,
    "time_period": "2024-01-01 to 2024-01-31"
  }
}

🔧 SDK Integration

Integrate EcoAI with popular AI frameworks

OpenAI Integration

from ecoai import EcoAI
import openai

eco = EcoAI(api_key="your_ecoai_key")
openai.api_key = "your_openai_key"

# Optimize prompt before sending to OpenAI
result = eco.optimize_prompt("Please kindly write a comprehensive analysis of the data")

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[{"role": "user", "content": result["optimized"]}]
)

print(f"Saved {result['tokens_saved']} tokens")
print(f"CO2 saved: {result['carbon_savings']['co2_g_saved']:.4f}g")

LangChain Integration

from ecoai import EcoAI
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

eco = EcoAI(api_key="your_ecoai_key")
llm = OpenAI(openai_api_key="your_openai_key")

# Optimize LangChain prompts
template = eco.optimize_prompt("Your template here")["optimized"]
prompt = PromptTemplate(template=template, input_variables=["input"])
chain = LLMChain(llm=llm, prompt=prompt)

result = chain.run("Your input here")

HuggingFace Integration

from ecoai import EcoAI
from transformers import pipeline

eco = EcoAI(api_key="your_ecoai_key")

# Optimize prompts for HuggingFace models
optimized_prompt = eco.optimize_prompt("Your prompt here")["optimized"]
classifier = pipeline("text-classification")
result = classifier(optimized_prompt)

⚠️ Error Handling

Common error responses and how to handle them

Error Response Format

{
  "success": false,
  "error": {
    "code": "INVALID_API_KEY",
    "message": "The provided API key is invalid",
    "details": "Please check your API key and try again"
  }
}

Common Error Codes

Code Status Description
INVALID_API_KEY 401 Invalid or missing API key
RATE_LIMIT_EXCEEDED 429 Rate limit exceeded
INVALID_REQUEST 400 Invalid request parameters
QUOTA_EXCEEDED 402 API quota exceeded

⏱️ Rate Limits

API usage limits and best practices

Rate Limit Tiers

Plan Requests per Minute Requests per Day Monthly Quota
Free 60 1,000 10,000
Pro 300 50,000 500,000
Enterprise 1,000 Unlimited Unlimited

Rate Limit Headers

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Reset: 1640995200

🚀 Ready to Get Started?

Download the EcoAI SDK and start optimizing your AI applications today

Get API Key