前提条件始める前に以下が必要です:
- 承認済みの App を含む開発者アカウント
- App の Bearer Token(Developer Console の「Keys and tokens」で確認できます)
1
投稿 ID を見つける
すべての投稿には一意の ID があります。投稿の URL から確認できます:
https://x.com/XDevelopers/status/1228393702244134912
└── これが投稿 ID
2
リクエストを実行する
cURL
curl "https://api.x.com/2/tweets/1228393702244134912" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# ID で単一の投稿を取得
response = client.posts.get("1228393702244134912")
print(response.data)
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
const response = await client.posts.get("1228393702244134912");
console.log(response.data);
3
レスポンスを確認する
デフォルトレスポンスには投稿の
id、text、edit_history_tweet_ids が含まれます:{
"data": {
"id": "1228393702244134912",
"text": "What did the developer write in their Valentine's card?\n\nwhile(true) {\n I = Love(You);\n}",
"edit_history_tweet_ids": ["1228393702244134912"]
}
}
4
追加フィールドをリクエストする
クエリパラメーターを使ってより多くのデータを取得します:レスポンス:
レスポンス例
cURL
curl "https://api.x.com/2/tweets/1228393702244134912?\
tweet.fields=created_at,public_metrics,author_id&\
expansions=author_id&\
user.fields=username,verified" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 追加フィールドと expansions 付きで投稿を取得
response = client.posts.get(
"1228393702244134912",
tweet_fields=["created_at", "public_metrics", "author_id"],
expansions=["author_id"],
user_fields=["username", "verified"]
)
print(response.data)
print(response.includes) # 作成者のユーザーオブジェクトを含む
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
const response = await client.posts.get("1228393702244134912", {
tweetFields: ["created_at", "public_metrics", "author_id"],
expansions: ["author_id"],
userFields: ["username", "verified"],
});
console.log(response.data);
console.log(response.includes); // 作成者のユーザーオブジェクトを含む
{
"data": {
"id": "1228393702244134912",
"text": "What did the developer write in their Valentine's card?...",
"created_at": "2020-02-14T19:00:55.000Z",
"author_id": "2244994945",
"public_metrics": {
"retweet_count": 156,
"reply_count": 23,
"like_count": 892,
"quote_count": 12
},
"edit_history_tweet_ids": ["1228393702244134912"]
},
"includes": {
"users": [
{
"id": "2244994945",
"username": "XDevelopers",
"verified": true
}
]
}
}
5
複数の投稿を取得する
1 回のリクエストで最大 100 件の投稿を取得します:
cURL
curl "https://api.x.com/2/tweets?\
ids=1228393702244134912,1227640996038684673,1199786642791452673&\
tweet.fields=created_at,author_id" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# ID を指定して複数の投稿を取得
response = client.posts.get_posts(
ids=["1228393702244134912", "1227640996038684673", "1199786642791452673"],
tweet_fields=["created_at", "author_id"]
)
for post in response.data:
print(f"{post.id}: {post.text[:50]}...")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
const response = await client.posts.getPosts({
ids: ["1228393702244134912", "1227640996038684673", "1199786642791452673"],
tweetFields: ["created_at", "author_id"],
});
response.data?.forEach((post) => {
console.log(`${post.id}: ${post.text?.slice(0, 50)}...`);
});
次のステップ
統合ガイド
認証、レート制限、ベストプラクティスを学ぶ
Fields と expansions
fields と expansions の仕組みをマスター
API リファレンス
利用可能なすべてのパラメーターを見る
サンプルコード
さらに多くの例を確認