|
| 1 | +# Upstash Context7 SDK |
| 2 | + |
| 3 | +`@upstash/context7-sdk` is an HTTP/REST based client for TypeScript, built on top of the [Context7 API](https://context7.com). |
| 4 | + |
| 5 | +## Why Context7? |
| 6 | + |
| 7 | +LLMs rely on outdated or generic training data about the libraries you use. This leads to: |
| 8 | + |
| 9 | +- Code examples based on year-old training data |
| 10 | +- Hallucinated APIs that don't exist |
| 11 | +- Generic answers for old package versions |
| 12 | + |
| 13 | +Context7 solves this by providing up-to-date, version-specific documentation and code examples directly from the source. Use this SDK to: |
| 14 | + |
| 15 | +- Build AI agents with accurate, current documentation context |
| 16 | +- Create RAG pipelines with reliable library documentation |
| 17 | +- Power code generation tools with real API references |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +### Install |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install @upstash/context7-sdk |
| 25 | +``` |
| 26 | + |
| 27 | +### Get API Key |
| 28 | + |
| 29 | +Get your API key from [Context7](https://context7.com) |
| 30 | + |
| 31 | +## Basic Usage |
| 32 | + |
| 33 | +```ts |
| 34 | +import { Context7 } from "@upstash/context7-sdk"; |
| 35 | + |
| 36 | +const client = new Context7({ |
| 37 | + apiKey: "<CONTEXT7_API_KEY>", |
| 38 | +}); |
| 39 | + |
| 40 | +// Search for libraries in the Context7 database |
| 41 | +const libraries = await client.searchLibrary("react"); |
| 42 | +console.log(libraries.results); |
| 43 | + |
| 44 | +// Query the documentation with specific topics |
| 45 | +const filteredDocs = await client.getDocs("/facebook/react", { |
| 46 | + topic: "hooks", |
| 47 | + limit: 10, |
| 48 | + page: 1, |
| 49 | +}); |
| 50 | + |
| 51 | +// Get documentation as JSON by default |
| 52 | +const docs = await client.getDocs("/vercel/next.js"); |
| 53 | +console.log(docs.snippets); |
| 54 | + |
| 55 | +// Get documentation as TXT |
| 56 | +const codeDocs = await client.getDocs("/mongodb/docs", { |
| 57 | + format: "txt", |
| 58 | + mode: "code", |
| 59 | +}); |
| 60 | +console.log(codeDocs.content); |
| 61 | +``` |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +### Environment Variables |
| 66 | + |
| 67 | +You can set your API key via environment variable: |
| 68 | + |
| 69 | +```sh |
| 70 | +CONTEXT7_API_KEY=ctx7sk-... |
| 71 | +``` |
| 72 | + |
| 73 | +Then initialize without options: |
| 74 | + |
| 75 | +```ts |
| 76 | +const client = new Context7(); |
| 77 | +``` |
| 78 | + |
| 79 | +## Docs |
| 80 | + |
| 81 | +See the [documentation](https://context7.com/docs/sdks/ts/getting-started) for details. |
| 82 | + |
| 83 | +## Contributing |
| 84 | + |
| 85 | +### Running tests |
| 86 | + |
| 87 | +```sh |
| 88 | +pnpm test |
| 89 | +``` |
| 90 | + |
| 91 | +### Building |
| 92 | + |
| 93 | +```sh |
| 94 | +pnpm build |
| 95 | +``` |
0 commit comments