Skip to main content
The getContext method retrieves documentation snippets from a specific library, ranked by relevance to your query. This is the core method for getting contextual documentation to feed to LLMs or display to users.

Method Signature

Parameters

string
required
The user’s question or task. Used to rank documentation snippets by relevance.Examples:
  • "How do I use the useState hook?"
  • "How to handle form validation?"
  • "Setting up middleware in Express"
string
required
Context7 library ID in the format /owner/repository.Examples:
  • "/facebook/react"
  • "/vuejs/core"
  • "/expressjs/express"
  • "/microsoft/typescript"
Get library IDs from the searchLibrary method’s results.
GetContextOptions
Configuration options for the request

Return Types

JSON Format (Default)

Returns an array of Documentation objects:
array
Array of documentation snippets, ordered by relevance to the query

Text Format

Returns a pre-formatted string with all documentation snippets, ready to be inserted into an LLM prompt or displayed to users.

Examples

Basic Usage (JSON)

Get structured documentation for a specific query:

Explicit JSON Format

Text Format for LLMs

Get pre-formatted documentation for language model prompts:

Building a Documentation Chatbot

RAG (Retrieval-Augmented Generation) Pattern

Displaying Documentation in UI

Combining Search and Context

Error Handling

Handling Invalid Library IDs

Caching Documentation

Server-Side API Route (Next.js)

Type Definitions

GetContextOptions

Documentation

Best Practices

Write Specific Queries

More specific queries return more targeted documentation:

Choose the Right Format

Use JSON when you need to process or display documentation:
Use text format when feeding directly to LLMs:

Handle Empty Results

Process Markdown Content

Documentation content may include markdown. Process it for display:

Common Library IDs

Here are some frequently used library IDs:
  • React: /facebook/react
  • Vue: /vuejs/core
  • Express: /expressjs/express
  • TypeScript: /microsoft/typescript
  • Next.js: /vercel/next.js
  • Svelte: /sveltejs/svelte
Use the searchLibrary method to discover library IDs programmatically.

Next Steps

Search Libraries

Learn how to find library IDs using searchLibrary

Client Configuration

Advanced client configuration options