# Search

> Find relevant documentation without resolving a library first

# Search

Search Context7 documentation in one request. Context7 selects relevant libraries, searches them in parallel, and returns the best snippets together.

The SDK returns structured JSON by default, consistent with its other methods. Set `type` to `txt` when you need plain text for an LLM prompt. The REST endpoint defaults to text unless you request `type=json`.

## Arguments

<ParamField path="query" type="string" required>
  The question or task to search for. Specific, natural language questions produce the best results.
</ParamField>

<ParamField path="options" type="SearchOptions">
  <Expandable title="properties">
    <ParamField path="libraries" type="string[]">
      Up to four library names or Context7 library IDs. These hints narrow the search when you already know which products are relevant.
    </ParamField>
    <ParamField path="version" type="string">
      A version to search. Provide at least one library hint. With one library hint, the version is strict and Search returns 404 if no matching tag exists. With multiple libraries, it is a contextual preference.
    </ParamField>
    <ParamField path="language" type="string">
      A programming language to prefer when ranking snippets, such as `TypeScript` or `Python`. This is a preference, so Context7 can still return product documentation when a language-specific snippet is unavailable.
    </ParamField>
    <ParamField path="type" type="'json' | 'txt'">
      The response format. The SDK default is `json`.
    </ParamField>
    <ParamField path="signal" type="AbortSignal">
      An abort signal for cancelling this request.
    </ParamField>
    <ParamField path="timeout" type="number | false">
      A timeout in milliseconds for this request. Use `false` to disable the client timeout.
    </ParamField>
    <ParamField path="cache" type="CacheSetting">
      A native fetch cache mode. Use `false` to omit the cache option.
    </ParamField>
  </Expandable>
</ParamField>

## Text response

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

const client = new Context7();

const context = await client.search(
  "How do I validate a request body with a schema?",
  { type: "txt" }
);

console.log(context);
```

## JSON response

JSON responses return a `SearchResponse` object with `codeSnippets`, `infoSnippets`, and optional `rules`. Each snippet includes the `libraryId` that it came from.

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

const client = new Context7();

const result = await client.search(
  "How do I stream an OpenAI response from a Next.js route?",
  {
    libraries: ["Next.js", "OpenAI"],
    language: "TypeScript",
  }
);

for (const snippet of result.codeSnippets) {
  console.log(snippet.libraryId, snippet.codeTitle);
  console.log(snippet.codeList);
}

for (const snippet of result.infoSnippets) {
  console.log(snippet.libraryId, snippet.content);
}

console.log(result.rules);
```

## Version-specific search

Use a library hint with `version` when your question depends on a particular release.

```typescript
const context = await client.search(
  "How do I cache the result of a server function?",
  {
    libraries: ["Next.js"],
    version: "15.4.0",
    language: "TypeScript",
  }
);
```

Library names do not need to be exact. Use a Context7 library ID when you need to select a specific documentation source.

If nothing matches, `client.search()` throws `Context7Error` with status 404. A temporary search failure returns status 503 and follows the SDK retry policy.
