Constructor
Create a new Context7 client instance:Parameters
Configuration object for the client
Returns
A configuredContext7 client instance.
Throws
Context7Error: If no API key is provided andCONTEXT7_API_KEYenvironment variable is not set
Configuration Options
Using Environment Variables
The recommended approach for production applications:Using Direct Configuration
Useful for testing or when you need multiple clients:Priority Order
When both methods are used, the constructor parameter takes precedence:Built-in Features
The client automatically configures several features:Automatic Retries
Failed requests are retried up to 5 times with exponential backoff:- Retry 1: ~50ms delay
- Retry 2: ~135ms delay
- Retry 3: ~403ms delay
- Retry 4: ~1.1s delay
- Retry 5: ~3s delay
Authentication
API key is automatically included in all requests as a Bearer token in the Authorization header.Cache Control
Requests are configured withcache: "no-store" to ensure fresh results.
Error Handling
Missing API Key
Invalid API Key Format
If your API key doesn’t start withctx7sk, you’ll see a warning:
Usage Examples
Basic Setup
Multiple Clients
Create separate clients for different API keys or use cases:Singleton Pattern
Reuse a single client instance across your application:Edge Runtime (Next.js, Cloudflare Workers)
The SDK works in edge runtimes:Type Definitions
Context7Config
Context7Error
Available Methods
Once initialized, the client provides two main methods:searchLibrary
Search for libraries by name and query
getContext
Retrieve documentation snippets for a library