context7

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context7

Context7

Overview

概述

Context7 is a service that provides LLMs with the latest library documentation. It prevents hallucinations from outdated training data or non-existent APIs, enabling retrieval of version-specific accurate documentation and code examples.
Context7是一项为大语言模型(LLMs)提供最新库文档的服务。它可以避免因过时训练数据或不存在的API产生的幻觉,支持检索特定版本的准确文档和代码示例。

When to Use

使用场景

Use this skill in the following cases:
  1. Explicit instruction: When the user instructs "use context7" or "check the latest documentation"
  2. Library usage questions: When asked about how to use a specific library's API, hooks, or functions
  3. Code example requests: When asked for code examples using a specific library
  4. Version-specific information: When library information for a specific version is needed
  5. Uncertain API information: When your knowledge might be outdated and latest information verification is needed
在以下场景中使用本Skill:
  1. 明确指令:当用户指示“use context7”或“查看最新文档”时
  2. 库使用问题:当被问及特定库的API、钩子(hooks)或函数的使用方法时
  3. 代码示例请求:当用户请求使用特定库的代码示例时
  4. 特定版本信息:当需要特定版本的库信息时
  5. 不确定的API信息:当你的知识可能过时,需要验证最新信息时

Workflow

工作流程

Step 1: Resolve Library ID

步骤1:解析库ID

First, obtain the Context7 ID for the target library.
API call:
bash
curl "https://context7.com/api/v2/libs/search?libraryName=LIBRARY_NAME&query=CONTEXT_QUERY" \
  -H "Authorization: Bearer $CONTEXT7_API_KEY"
Example:
bash
undefined
首先,获取目标库的Context7 ID。
API调用:
bash
curl "https://context7.com/api/v2/libs/search?libraryName=LIBRARY_NAME&query=CONTEXT_QUERY" \
  -H "Authorization: Bearer $CONTEXT7_API_KEY"
示例:
bash
undefined

Search for React library

搜索React库

curl "https://context7.com/api/v2/libs/search?libraryName=react&query=hooks"
-H "Authorization: Bearer $CONTEXT7_API_KEY"

**Criteria for selecting from response:**
- `trustScore`: Trust score (higher is better)
- `totalSnippets`: Number of available documents (more means richer information)
- `versions`: Verify that the required version is included
curl "https://context7.com/api/v2/libs/search?libraryName=react&query=hooks"
-H "Authorization: Bearer $CONTEXT7_API_KEY"

**响应结果选择标准:**
- `trustScore`:信任分数(越高越好)
- `totalSnippets`:可用文档数量(越多表示信息越丰富)
- `versions`:验证是否包含所需版本

Step 2: Retrieve Documentation

步骤2:检索文档

Use the resolved library ID to retrieve specific documentation.
API call:
bash
curl "https://context7.com/api/v2/context?libraryId=LIBRARY_ID&query=SPECIFIC_QUERY" \
  -H "Authorization: Bearer $CONTEXT7_API_KEY"
Example:
bash
undefined
使用解析得到的库ID检索特定文档。
API调用:
bash
curl "https://context7.com/api/v2/context?libraryId=LIBRARY_ID&query=SPECIFIC_QUERY" \
  -H "Authorization: Bearer $CONTEXT7_API_KEY"
示例:
bash
undefined

Retrieve information about React's useEffect

检索React的useEffect相关信息

curl "https://context7.com/api/v2/context?libraryId=/facebook/react&query=useEffect cleanup function"
-H "Authorization: Bearer $CONTEXT7_API_KEY"

**Query best practices:**
- Use specific queries ("useEffect cleanup function" rather than "hooks")
- Be clear about the purpose ("authentication middleware", "form validation", etc.)
curl "https://context7.com/api/v2/context?libraryId=/facebook/react&query=useEffect cleanup function"
-H "Authorization: Bearer $CONTEXT7_API_KEY"

**查询最佳实践:**
- 使用具体的查询词(比如“useEffect cleanup function”而非“hooks”)
- 明确查询目的(比如“认证中间件”、“表单验证”等)

Step 3: Respond to User

步骤3:响应用户

Respond to the user's question based on the retrieved documentation.
Information to include in the response:
  • Retrieved code examples
  • API explanations
  • Documentation source URLs (for reference)
根据检索到的文档响应用户的问题。
响应中需包含的信息:
  • 检索到的代码示例
  • API说明
  • 文档来源URL(供参考)

Examples

示例

Example 1: React Hook Usage

示例1:React Hook使用

User: "How do I write a cleanup function with React's useEffect?"
Execution steps:
  1. Library search:
    libraryName=react
    ,
    query=useEffect cleanup
  2. Documentation retrieval:
    libraryId=/facebook/react
    ,
    query=useEffect cleanup function
  3. Respond with the latest code examples and best practices
用户:“如何使用React的useEffect编写清理函数?”
执行步骤:
  1. 库搜索:
    libraryName=react
    query=useEffect cleanup
  2. 文档检索:
    libraryId=/facebook/react
    query=useEffect cleanup function
  3. 返回最新的代码示例和最佳实践

Example 2: Next.js Routing

示例2:Next.js路由

User: "Use context7 to show me how to implement authentication middleware with Next.js App Router"
Execution steps:
  1. Library search:
    libraryName=next.js
    ,
    query=middleware authentication
  2. Documentation retrieval:
    libraryId=/vercel/next.js
    ,
    query=middleware authentication route protection
  3. Respond with the latest App Router compatible middleware implementation
用户:“Use context7展示如何用Next.js App Router实现认证中间件”
执行步骤:
  1. 库搜索:
    libraryName=next.js
    query=middleware authentication
  2. 文档检索:
    libraryId=/vercel/next.js
    query=middleware authentication route protection
  3. 返回与App Router兼容的最新中间件实现方案

Example 3: Multiple Libraries Combination

示例3:多库组合

User: "How to create a button component using TailwindCSS and shadcn/ui"
Execution steps:
  1. Resolve IDs for each library
  2. Retrieve documentation for both
  3. Respond with a combined implementation example
用户:“如何使用TailwindCSS和shadcn/ui创建按钮组件?”
执行步骤:
  1. 解析每个库的ID
  2. 检索两个库的文档
  3. 返回组合实现的示例

Error Handling

错误处理

ErrorSolution
404 (Not Found)Search again with a different library name (e.g., "nextjs" → "next.js")
429 (Rate Limit)Wait a moment and retry
Empty responseRetry with a more general query
错误解决方案
404 (未找到)使用不同的库名称重新搜索(例如“nextjs” → “next.js”)
429 (请求频率限制)稍等片刻后重试
空响应使用更通用的查询词重试

Resources

资源

For detailed API specifications, see
references/api_reference.md
.
  • Endpoint details
  • Response field descriptions
  • Rate limit information
  • Error code list
有关详细的API规范,请参阅
references/api_reference.md
  • 端点详情
  • 响应字段说明
  • 请求频率限制信息
  • 错误代码列表