> ## Documentation Index
> Fetch the complete documentation index at: https://context7.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Context

> Retrieve documentation context for a library

# Get Context

Retrieve documentation context for a specific library. Returns documentation as a JSON array of documentation snippets (default) or as plain text.

## Arguments

**query** `string` *required*

The user's question or task (used for relevance ranking)

**libraryId** `string` *required*

The library ID (e.g., `/facebook/react`)

**options** `GetContextOptions`

**properties**

**type** `'json' | 'txt'`

Format of the response

* `json`: Array of Documentation objects (default)
* `txt`: Plain text format

Default: `"json"`

**signal** `AbortSignal`

Abort signal for cancelling this request.

**timeout** `number | false`

Per-request timeout in milliseconds. Use `false` to disable the client timeout.

**cache** `CacheSetting`

Native fetch cache mode. Use `false` to omit the cache option.

## Response

The response type depends on the `type` option.

### JSON Format (`type: "json"` or default)

Returns `Documentation[]` - an array of documentation objects.

### Text Format (`type: "txt"`)

Returns a `string` containing the documentation context ready to use in LLM prompts.

**Documentation** `object`

**properties**

**title** `string` *required*

Title of the documentation snippet

**content** `string` *required*

The documentation content (includes code blocks in markdown format)

**source** `string` *required*

Source identifier for the snippet

## Examples

```typescript JSON Format (Default) theme={null}
import { Context7 } from "@upstash/context7-sdk";

const client = new Context7();

const docs = await client.getContext(
  "How do I use hooks?",
  "/react/react"
);

docs.forEach((doc) => {
  console.log(doc.title);
  console.log(doc.content);
  console.log(doc.source);
});
```

```typescript Text Format theme={null}
import { Context7 } from "@upstash/context7-sdk";

const client = new Context7();

const context = await client.getContext(
  "How do I use hooks?",
  "/react/react",
  { type: "txt" }
);

console.log(context);
```

```typescript Error Handling theme={null}
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;
  }
}
```

## Use Cases

### Providing Context to LLMs

```typescript theme={null}
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("/react/react", "How do I use useEffect?");
```

### Processing Individual Snippets

```typescript theme={null}
import { Context7 } from "@upstash/context7-sdk";

const client = new Context7();

const docs = await client.getContext(
  "How to create components?",
  "/react/react",
  { type: "json" }
);

docs.forEach((doc) => {
  console.log(`Title: ${doc.title}`);
  console.log(`Source: ${doc.source}`);
  console.log(`Content length: ${doc.content.length} chars`);
});
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.