x-api
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseX API: Query the X (Twitter) API v2 with curl
X API:使用curl查询X(Twitter)API v2
Overview
概述
Use curl commands to interact with the X (Twitter) API v2. This skill covers read-only, public endpoints that do not require an authenticated user context — things like looking up profiles, searching recent posts, and retrieving tweets.
Authentication assumption: The environment variable must already be set with a valid X API v2 Bearer Token. All commands below rely on it.
X_BEARER_TOKEN使用curl命令与X(Twitter)API v2进行交互。本技能涵盖无需用户上下文认证的只读公开端点——例如查询资料、搜索近期帖子、获取推文等操作。
认证前提: 环境变量必须已设置有效的X API v2 Bearer Token。以下所有命令均依赖该变量。
X_BEARER_TOKENHow to use
使用方法
All requests follow this pattern:
bash
curl "<endpoint_url>" \
-H "Authorization: Bearer $X_BEARER_TOKEN"For any endpoint that returns tweets, always include in so long posts return full content.
note_tweettweet.fields所有请求遵循以下格式:
bash
curl "<endpoint_url>" \
-H "Authorization: Bearer $X_BEARER_TOKEN"对于任何返回推文的端点,请务必在中包含,这样长帖子会返回完整内容。
tweet.fieldsnote_tweetExamples
示例
Get a user profile
获取用户资料
bash
curl "https://api.x.com/2/users/by/username/xdevelopers" \
-H "Authorization: Bearer $X_BEARER_TOKEN"bash
curl "https://api.x.com/2/users/by/username/xdevelopers" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Get a user profile with additional fields
获取包含额外字段的用户资料
bash
curl "https://api.x.com/2/users/by/username/xdevelopers?user.fields=created_at,description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Common : , , , , , , , , , , , , , .
user.fieldscreated_atdescriptionentitiesidlocationnamepinned_tweet_idprofile_image_urlprotectedpublic_metricsurlusernameverifiedwithheldbash
curl "https://api.x.com/2/users/by/username/xdevelopers?user.fields=created_at,description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"常用字段:, , , , , , , , , , , , , 。
user.fieldscreated_atdescriptionentitiesidlocationnamepinned_tweet_idprofile_image_urlprotectedpublic_metricsurlusernameverifiedwithheldLook up a post by ID
通过ID查询帖子
bash
curl "https://api.x.com/2/tweets/1460323737035677698?tweet.fields=note_tweet,created_at,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Common : , , , , , , , , , , , , , .
tweet.fieldsnote_tweetattachmentsauthor_idconversation_idcreated_atentitiesidin_reply_to_user_idlangpublic_metricsreferenced_tweetssourcetextwithheldbash
curl "https://api.x.com/2/tweets/1460323737035677698?tweet.fields=note_tweet,created_at,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"常用字段:, , , , , , , , , , , , , 。
tweet.fieldsnote_tweetattachmentsauthor_idconversation_idcreated_atentitiesidin_reply_to_user_idlangpublic_metricsreferenced_tweetssourcetextwithheldSearch recent posts
搜索近期帖子
bash
curl "https://api.x.com/2/tweets/search/recent?query=from:xdevelopers&tweet.fields=note_tweet,created_at" \
-H "Authorization: Bearer $X_BEARER_TOKEN"The parameter supports the full X search query syntax. Examples:
query- — posts from a specific user
from:username - — replies to a specific user
to:username - — posts containing an exact phrase
"exact phrase" - — posts containing both keywords
keyword1 keyword2 - — posts containing either keyword
keyword1 OR keyword2 - — posts with a specific hashtag
#hashtag - — posts that contain media
has:media - — posts that contain links
has:links - — posts in a specific language
lang:en
bash
curl "https://api.x.com/2/tweets/search/recent?query=from:xdevelopers&tweet.fields=note_tweet,created_at" \
-H "Authorization: Bearer $X_BEARER_TOKEN"query- — 特定用户发布的帖子
from:username - — 回复特定用户的帖子
to:username - — 包含精确短语的帖子
"exact phrase" - — 同时包含两个关键词的帖子
keyword1 keyword2 - — 包含任意一个关键词的帖子
keyword1 OR keyword2 - — 包含特定话题标签的帖子
#hashtag - — 包含媒体内容的帖子
has:media - — 包含链接的帖子
has:links - — 特定语言的帖子
lang:en
Get a user's posts
获取用户发布的帖子
bash
curl "https://api.x.com/2/users/2244994945/tweets?max_results=5&tweet.fields=note_tweet" \
-H "Authorization: Bearer $X_BEARER_TOKEN"You can add more , (5-100), and pagination tokens to customize the response, but always keep included.
tweet.fieldsmax_resultsnote_tweetbash
curl "https://api.x.com/2/users/2244994945/tweets?max_results=5&tweet.fields=note_tweet" \
-H "Authorization: Bearer $X_BEARER_TOKEN"你可以添加更多、(取值范围5-100)和分页令牌来自定义响应,但请务必保留字段。
tweet.fieldsmax_resultsnote_tweetGet a user's mentions
获取用户的提及帖子
bash
curl "https://api.x.com/2/users/2244994945/mentions?max_results=5&tweet.fields=note_tweet,created_at,author_id" \
-H "Authorization: Bearer $X_BEARER_TOKEN"bash
curl "https://api.x.com/2/users/2244994945/mentions?max_results=5&tweet.fields=note_tweet,created_at,author_id" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Look up multiple users by username
通过用户名批量查询用户
bash
curl "https://api.x.com/2/users/by?usernames=xdevelopers,twitterdev&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"bash
curl "https://api.x.com/2/users/by?usernames=xdevelopers,twitterdev&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Get tweet counts for a search query
获取搜索查询的推文数量
bash
curl "https://api.x.com/2/tweets/counts/recent?query=from:xdevelopers" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Returns the count of tweets matching the query, grouped by time period.
bash
curl "https://api.x.com/2/tweets/counts/recent?query=from:xdevelopers" \
-H "Authorization: Bearer $X_BEARER_TOKEN"返回匹配查询条件的推文数量,按时间段分组统计。
Get a user's followers
获取用户的关注者
bash
curl "https://api.x.com/2/users/2244994945/followers?max_results=10&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"bash
curl "https://api.x.com/2/users/2244994945/followers?max_results=10&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Get accounts a user is following
获取用户关注的账号
bash
curl "https://api.x.com/2/users/2244994945/following?max_results=10&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"bash
curl "https://api.x.com/2/users/2244994945/following?max_results=10&user.fields=description,public_metrics" \
-H "Authorization: Bearer $X_BEARER_TOKEN"Look up a list and its members
查询列表及其成员
bash
undefinedbash
undefinedGet list details
获取列表详情
curl "https://api.x.com/2/lists/84839422"
-H "Authorization: Bearer $X_BEARER_TOKEN"
-H "Authorization: Bearer $X_BEARER_TOKEN"
curl "https://api.x.com/2/lists/84839422"
-H "Authorization: Bearer $X_BEARER_TOKEN"
-H "Authorization: Bearer $X_BEARER_TOKEN"
Get list members
获取列表成员
curl "https://api.x.com/2/lists/84839422/members?user.fields=description,public_metrics"
-H "Authorization: Bearer $X_BEARER_TOKEN"
-H "Authorization: Bearer $X_BEARER_TOKEN"
undefinedcurl "https://api.x.com/2/lists/84839422/members?user.fields=description,public_metrics"
-H "Authorization: Bearer $X_BEARER_TOKEN"
-H "Authorization: Bearer $X_BEARER_TOKEN"
undefinedTips
提示
- To find a user's numeric ID (needed for some endpoints), first look them up by username, then use the field from the response.
id - Use to include related objects in the response. For example,
expansionson a tweet lookup will include the author's user object.expansions=author_id - Pagination: When results span multiple pages, the response includes a field. Pass it as
next_tokenin the next request.pagination_token - Rate limits vary by endpoint. If you receive a 429 response, wait before retrying.
- 若要获取用户的数字ID(部分端点需要),请先通过用户名查询用户,然后从响应中提取字段。
id - 使用参数可在响应中包含关联对象。例如,在推文查询中添加
expansions会包含作者的用户对象。expansions=author_id - 分页:当结果跨多页时,响应会包含字段。将其作为
next_token参数传入下一次请求即可。pagination_token - 不同端点的速率限制不同。若收到429响应,请等待后再重试。
API Reference
API参考
Full OpenAPI specification: https://api.x.com/2/openapi.json
完整OpenAPI规范:https://api.x.com/2/openapi.json
Authentication
认证
The environment variable must be set with a valid Bearer Token. This token provides app-only authentication, which grants access to public read-only endpoints.
X_BEARER_TOKENTo obtain a Bearer Token, create a project and app in the X Developer Portal and generate a Bearer Token from your app's "Keys and tokens" settings.
环境变量必须设置有效的Bearer Token。该令牌提供应用级认证,可访问只读公开端点。
X_BEARER_TOKEN要获取Bearer Token,请在X开发者平台创建项目和应用,然后从应用的“密钥与令牌”设置中生成Bearer Token。