Skip to main content

Get Docs

Retrieve documentation for a specific library. You can get code examples or informational documentation in either JSON or text format.

Arguments

libraryId
string
required
The library identifier in the format /owner/repo (e.g., /facebook/react)
options
GetDocsOptions

Response

The response type is automatically inferred based on the format and mode parameters. All response types extend a common base with pagination and token information.

Text Format (format: "txt")

TextDocsResponse
object

JSON Format with Code Type (format: "json", mode: "code")

CodeDocsResponse
object

JSON Format with Info Type (format: "json", mode: "info")

InfoDocsResponse
object

Examples

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

const client = new Context7();

// JSON is the default format, no need to specify
const docs = await client.getDocs("/facebook/react", {
  mode: "code",
  limit: 5,
});

console.log(`Retrieved ${docs.snippets.length} code snippets`);

Use Cases

Building a Documentation Chat Bot

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

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

async function getRelevantDocs(library: string, topic: string) {
  const docs = await client.getDocs(library, {
    mode: "code",
    format: "json",
    topic,
    limit: 5,
  });

  return docs.snippets.map((snippet) => ({
    title: snippet.codeTitle,
    description: snippet.codeDescription,
    code: snippet.codeList[0]?.code,
    language: snippet.codeLanguage,
  }));
}

const reactHooksDocs = await getRelevantDocs("/facebook/react", "hooks");

Fetching All Documentation Pages

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

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

async function getAllDocs(library: string) {
  const allSnippets = [];
  let page = 1;
  let hasMore = true;

  while (hasMore) {
    const response = await client.getDocs(library, {
      mode: "code",
      format: "json",
      page,
      limit: 100,
    });

    allSnippets.push(...response.snippets);
    hasMore = response.pagination.hasNext;
    page++;
  }

  return allSnippets;
}

const allReactDocs = await getAllDocs("/facebook/react");
console.log(`Total snippets: ${allReactDocs.length}`);

Extracting Code Examples

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

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

const docs = await client.getDocs("/facebook/react", {
  mode: "code",
  format: "json",
  limit: 20,
});

const tsExamples = docs.snippets
  .filter((snippet) => snippet.codeLanguage === "typescript")
  .map((snippet) => snippet.codeList)
  .flat();

console.log(`Found ${tsExamples.length} TypeScript examples`);