docs/sdks/ts/commands/get-context.mdx
Retrieve documentation context for a specific library. Returns documentation as a JSON array of documentation snippets (default) or as plain text.
Default: `"json"`
</ParamField>
The response type depends on the type option.
type: "json" or default)Returns Documentation[] - an array of documentation objects.
type: "txt")Returns a string containing the documentation context ready to use in LLM prompts.
const client = new Context7();
const docs = await client.getContext( "How do I use hooks?", "/facebook/react" );
docs.forEach((doc) => { console.log(doc.title); console.log(doc.content); console.log(doc.source); });
```typescript Text Format
import { Context7 } from "@upstash/context7-sdk";
const client = new Context7();
const context = await client.getContext(
"How do I use hooks?",
"/facebook/react",
{ type: "txt" }
);
console.log(context);
import { Context7, Context7Error } from "@upstash/context7-sdk";
const client = new Context7();
try {
const context = await client.getContext(
"How to get started?",
"/invalid/library"
);
} catch (error) {
if (error instanceof Context7Error) {
console.error("API Error:", error.message);
} else {
throw error;
}
}
import { Context7 } from "@upstash/context7-sdk";
const client = new Context7();
async function getDocsForPrompt(library: string, question: string) {
const context = await client.getContext(question, library);
return `
Here is the relevant documentation:
${context}
User question: ${question}
`;
}
const prompt = await getDocsForPrompt("/facebook/react", "How do I use useEffect?");
import { Context7 } from "@upstash/context7-sdk";
const client = new Context7();
const docs = await client.getContext(
"How to create components?",
"/facebook/react",
{ type: "json" }
);
docs.forEach((doc) => {
console.log(`Title: ${doc.title}`);
console.log(`Source: ${doc.source}`);
console.log(`Content length: ${doc.content.length} chars`);
});