API Guide - Context7 MCP
Skip to main content

Authentication

All API requests require authentication using an API key. Include your API key in the Authorization header:
Authorization: Bearer CONTEXT7_API_KEY
Get your API key at context7.com/dashboard. Learn more about creating and managing API keys.

API Methods

Complete Workflow Example

import requests

headers = {"Authorization": "Bearer CONTEXT7_API_KEY"}

# Step 1: Search for the library
search_response = requests.get(
    "https://context7.com/api/v2/libs/search",
    headers=headers,
    params={"libraryName": "react", "query": "I need to manage state"}
)
data = search_response.json()
best_match = data["results"][0]
print(f"Found: {best_match['title']} ({best_match['id']})")

# Step 2: Get documentation context
context_response = requests.get(
    "https://context7.com/api/v2/context",
    headers=headers,
    params={"libraryId": best_match["id"], "query": "How do I use useState?", "type": "json"}
)
docs = context_response.json()

for snippet in docs["codeSnippets"]:
    print(f"Title: {snippet['codeTitle']}")
    for code in snippet["codeList"]:
        print(f"Code: {code['code'][:200]}...")

for info in docs["infoSnippets"]:
    print(f"Content: {info['content'][:200]}...")
For TypeScript SDK usage, see Search Library and Get Context.

Rate Limits

  • Without API key: Low rate limits and no custom configuration
  • With API key: Higher limits based on your plan
  • View current usage and reset windows in the dashboard.
When you exceed rate limits, the API returns a 429 status code with these headers:
HeaderDescription
Retry-AfterSeconds until rate limit resets
RateLimit-LimitTotal request limit
RateLimit-RemainingRemaining requests in window
RateLimit-ResetUnix timestamp when limit resets

Best Practices

Be Specific with Queries

Use detailed, natural language queries for better results:
# Good - specific question
curl "https://context7.com/api/v2/context?libraryId=/vercel/next.js&query=How%20to%20implement%20authentication%20with%20middleware" \
  -H "Authorization: Bearer CONTEXT7_API_KEY"

# Less optimal - vague query
curl "https://context7.com/api/v2/context?libraryId=/vercel/next.js&query=auth" \
  -H "Authorization: Bearer CONTEXT7_API_KEY"

Cache Responses

Documentation updates are relatively infrequent, so caching responses for several hours or days reduces API calls and improves performance.

Handle Rate Limits

Implement exponential backoff for rate limit errors:
import time
import requests

def fetch_with_retry(url, headers, max_retries=3):
    for attempt in range(max_retries):
        response = requests.get(url, headers=headers)

        if response.status_code == 429:
            retry_after = int(response.headers.get("Retry-After", 2 ** attempt))
            time.sleep(retry_after)
            continue

        return response

    raise Exception("Max retries exceeded")

Use Specific Versions

Pin to a specific version for consistent results. Both / and @ syntax are supported:
curl "https://context7.com/api/v2/context?libraryId=/vercel/next.js/v15.1.8&query=app%20router" \
  -H "Authorization: Bearer CONTEXT7_API_KEY"

curl "https://context7.com/api/v2/context?libraryId=/vercel/next.js@v15.1.8&query=app%20router" \
  -H "Authorization: Bearer CONTEXT7_API_KEY"

Error Handling

The Context7 API uses standard HTTP status codes:
All errors return a JSON object with error and message fields:
{
  "error": "library_not_found",
  "message": "Library \"/owner/repo\" not found. Please check the library ID or your access permissions."
}
For 301 redirects, the response also includes a redirectUrl field pointing to the new library ID.

SDK and Libraries

For TypeScript SDK installation and usage, see the Getting Started guide.