Skip to main content

Getting Started

@upstash/context7-sdk is a TypeScript SDK for Context7, enabling easier access to library documentation with full type coverage. Using @upstash/context7-sdk you can:
  • Search across available libraries
  • Retrieve code documentation with contextual examples
  • Fetch informational documentation and guides
  • Access library metadata and versioning information
  • Filter documentation by topic and pagination
You can find the Github Repository here.

Install

npm install @upstash/context7-sdk

Usage

Initializing the Client

To use the Context7 SDK, you need an API key. You can get your API key from the Context7 Dashboard.

Using environment variables

The SDK automatically reads from environment variables if no API key is provided in the config:
CONTEXT7_API_KEY="your_api_key_here"
When an environment variable is set, you can initialize the client without any parameters:
import { Context7 } from "@upstash/context7-sdk";

const client = new Context7();

Using a configuration object

If you prefer to pass configuration in code, the constructor accepts a config object containing the apiKey value. This could be useful if your application needs to interact with multiple projects, each with a different configuration.
import { Context7 } from "@upstash/context7-sdk";

const client = new Context7({
  apiKey: <CONTEXT7_API_KEY>,
});
The SDK checks for API keys in this order: 1. config.apiKey (if provided) 2. process.env.CONTEXT7_API_KEY

Quick Start Example

import { Context7 } from "@upstash/context7-sdk";

const client = new Context7();

const searchResponse = await client.searchLibrary("react");
console.log(`Found ${searchResponse.results.length} libraries`);

// Get code documentation (JSON is the default format)
const codeDocs = await client.getDocs("/facebook/react", {
  mode: "code",
  limit: 5,
});

console.log(`Retrieved ${codeDocs.snippets.length} code snippets`);
console.log(`Total tokens: ${codeDocs.totalTokens}`);

// Get info documentation as text
const textDocs = await client.getDocs("/facebook/react", {
  mode: "info",
  format: "txt",
  limit: 3,
});

console.log(textDocs.content);

Error Handling

The SDK throws Context7Error for API errors:
import { Context7, Context7Error } from "@upstash/context7-sdk";

const client = new Context7({
  apiKey: process.env.CONTEXT7_API_KEY!,
});

try {
  const docs = await client.getDocs("/invalid/library", {
    mode: "code",
    format: "txt",
  });
} catch (error) {
  if (error instanceof Context7Error) {
    console.error("Context7 API Error:", error.message);
  } else {
    console.error("Unexpected error:", error);
  }
}

Next Steps

Explore the SDK commands: