API Testing with OpenRouter: Free Endpoint Testing Tutorial
Learn how to test API endpoints using OpenRouter's free AI models - perfect for validating API responses and generating test cases.
Introduction
OpenRouter provides free access to multiple AI models through a unified API. This tutorial demonstrates how to use OpenRouter for API testing tasks: validating responses, generating test data, and creating test cases - all without API costs.
Prerequisites
- Python 3.8+
-
requestslibrary:pip install requests - OpenRouter account (free at openrouter.ai)
- API key from OpenRouter dashboard
Quick Start: Testing OpenRouter API
import requests import os import json OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions" def test_openrouter(prompt: str, model: str = "google/gemini-2.0-flash-exp:free") -> str: """Test OpenRouter API with a prompt.""" headers = { "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", "Content-Type": "application/json" } data = { "model": model, "messages": [{"role": "user", "content": prompt}], "temperature": 0.7, "max_tokens": 500 } response = requests.post(OPENROUTER_URL, headers=headers, json=data, timeout=30) if response.status_code == 200: return response.json()["choices"][0]["message"]["content"] else: raise Exception(f"Error: {response.status_code} - {response.text}") # Test it if __name__ == "__main__": prompt = "Explain REST API in one sentence" try: result = test_openrouter(prompt) print(f"Q: {prompt}") print(f"A: {result}") except Exception as e: print(f"Error: {e}") API Testing Use Cases
1. Generate Test Data for APIs
def generate_test_data(endpoint_type: str, count: int = 5) -> list: """Generate realistic test data for API testing.""" prompt = f"""Generate {count} realistic JSON test objects for {endpoint_type} API. Include: valid data, edge cases, boundary values. Return as JSON array.""" result = test_openrouter(prompt) return json.loads(result) # Example: Generate user data users = generate_test_data("user registration", 3) for user in users: print(json.dumps(user, indent=2)) 2. Create API Test Cases
def generate_test_cases(endpoint: str, method: str) -> list: """Generate comprehensive test cases for an API endpoint.""" prompt = f"""Create 5 test cases for {method} {endpoint}. Include: positive, negative, edge case, boundary, auth test. Return JSON array with: name, input, expected_status, expected_fields.""" result = test_openrouter(prompt) return json.loads(result) # Test cases for GET /users/:id cases = generate_test_cases("/users/:id", "GET") for case in cases: print(f"{case['name']}: expect {case['expected_status']}") 3. Validate API Responses
def validate_response(expected: dict, actual: dict) -> dict: """Use AI to validate API response matches expectations.""" prompt = f"""Compare expected vs actual API response. Expected: {json.dumps(expected)} Actual: {json.dumps(actual)} Return: {{"match": bool, "differences": [], "severity": "low|medium|high"}}""" result = test_openrouter(prompt) return json.loads(result) Free Models Available
| Model | Best For |
|---|---|
google/gemini-2.0-flash-exp:free | General testing, code |
meta-llama/llama-3.1-8b-instruct:free | Reasoning, analysis |
mistralai/mistral-7b-instruct:free | Fast responses |
microsoft/phi-3-mini-128k-instruct:free | Long context |
Complete Test Automation Example
class APITestAutomator: def __init__(self, base_url: str): self.base_url = base_url self.session = requests.Session() def test_endpoint(self, endpoint: str, method: str = "GET", **kwargs) -> dict: url = f"{self.base_url}{endpoint}" response = self.session.request(method, url, timeout=10, **kwargs) return { "endpoint": endpoint, "method": method, "status": response.status_code, "time": response.elapsed.total_seconds(), "data": response.json() if "json" in response.headers.get("content-type", "") else response.text } def ai_generate_tests(self, endpoint: str, method: str) -> list: """Use AI to generate test cases.""" return generate_test_cases(endpoint, method) def run_ai_tests(self, endpoint: str, method: str) -> list: """Run AI-generated test cases.""" cases = self.ai_generate_tests(endpoint, method) results = [] for case in cases: result = self.test_endpoint(case.get("input", endpoint), case.get("method", method)) result["test_name"] = case["name"] result["passed"] = result["status"] == case.get("expected_status", 200) results.append(result) return results # Usage if __name__ == "__main__": automator = APITestAutomator("https://jsonplaceholder.typicode.com") results = automator.run_ai_tests("/posts", "GET") for r in results: status = "PASS" if r["passed"] else "FAIL" print(f"{r['test_name']}: {status} - {r['status']} ({r['time']:.3f}s)") Error Handling & Rate Limits
import time def robust_call(prompt: str, retries: int = 3) -> str: """Call with exponential backoff for rate limits.""" for attempt in range(retries): try: return test_openrouter(prompt) except Exception as e: if "429" in str(e) or "503" in str(e): wait = 2 ** attempt print(f"Rate limited. Waiting {wait}s...") time.sleep(wait) else: raise raise Exception("Max retries exceeded") Best Practices
- Use environment variables for API keys
- Set timeouts (30s for AI, 10s for API under test)
- Monitor rate limits - Free tier has limits
- Cache AI responses during development
- Validate AI output before using in tests
- Log everything for debugging
Conclusion
OpenRouter's free models provide powerful AI assistance for API testing automation. Generate test data, create test cases, validate responses, and build complete test suites - all at zero cost. Perfect for developers learning API testing or teams wanting AI-augmented QA.
Published as part of the AI Tutorials series on Dev.to