docs/sdks/ts/getting-started.mdx
@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:
You can find the Github Repository here.
pnpm add @upstash/context7-sdk
yarn add @upstash/context7-sdk
bun add @upstash/context7-sdk
To use the Context7 SDK, you need an API key. You can get your API key from the Context7 Dashboard.
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();
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>,
});
import { Context7 } from "@upstash/context7-sdk";
const client = new Context7();
// Search for libraries
const libraries = await client.searchLibrary(
"I need to build a UI with components",
"react"
);
console.log(`Found ${libraries.length} libraries`);
console.log(libraries[0].id); // "/facebook/react"
// Get documentation as JSON array (default)
const docs = await client.getContext(
"How do I use hooks?",
"/facebook/react"
);
console.log(docs[0].title, docs[0].content);
// Get documentation context as plain text
const context = await client.getContext("How do I use hooks?", "/facebook/react", {
type: "txt",
});
console.log(context);
The SDK throws Context7Error for API errors:
import { Context7, Context7Error } from "@upstash/context7-sdk";
const client = new Context7();
try {
const context = await client.getContext("query", "/invalid/library");
} catch (error) {
if (error instanceof Context7Error) {
console.error("Context7 API Error:", error.message);
} else {
console.error("Unexpected error:", error);
}
}
Explore the SDK commands: