ポストを作成する
1
リクエストを準備する
POST
/2/tweets エンドポイントは、少なくとも text または media のいずれかを含む JSON ボディを必要とします。{
"text": "Hello from the X API!"
}
2
リクエストを送信する
cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"text": "Hello from the X API!"}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# ポストを作成する
response = client.posts.create(text="Hello from the X API!")
print(f"Created Post: {response.data.id}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// ポストを作成する
const response = await client.posts.create({ text: "Hello from the X API!" });
console.log(`Created Post: ${response.data?.id}`);
3
レスポンスを確認する
正常なレスポンスには、新しいポストの
id と text が含まれます。{
"data": {
"id": "1445880548472328192",
"text": "Hello from the X API!"
}
}
高度な例
ポストに返信する
ポストに返信する
cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "これは返信です!",
"reply": {
"in_reply_to_tweet_id": "1234567890"
}
}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# 返信を作成
response = client.posts.create(
text="これは返信です!",
reply={"in_reply_to_tweet_id": "1234567890"}
)
print(f"返信を作成しました: {response.data.id}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// 返信を作成
const response = await client.posts.create({
text: "これは返信です!",
reply: { inReplyToTweetId: "1234567890" },
});
console.log(`返信を作成しました: ${response.data?.id}`);
ポストを引用する
ポストを引用する
cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "これ見て!",
"quote_tweet_id": "1234567890"
}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# ポストを引用する
response = client.posts.create(
text="これ見て!",
quote_tweet_id="1234567890"
)
print(f"引用ポストを作成しました: {response.data.id}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// ポストを引用する
const response = await client.posts.create({
text: "これ見て!",
quoteTweetId: "1234567890",
});
console.log(`引用ポストを作成しました: ${response.data?.id}`);
メディア付きポスト
メディア付きポスト
まずは Media Upload エンドポイント を使ってメディアをアップロードし、その後に
media_id を指定します。cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "Photo of the day!",
"media": {
"media_ids": ["1234567890123456789"]
}
}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# メディア付きでポストする
response = client.posts.create(
text="Photo of the day!",
media={"media_ids": ["1234567890123456789"]}
)
print(f"Created Post with media: {response.data.id}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// メディア付きでポストする
const response = await client.posts.create({
text: "Photo of the day!",
media: { mediaIds: ["1234567890123456789"] },
});
console.log(`Created Post with media: ${response.data?.id}`);
投票付きポスト
投票付きポスト
cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "あなたの好きな色は何ですか?",
"poll": {
"options": ["赤", "青", "緑", "黄色"],
"duration_minutes": 1440
}
}'
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# 投票付きのポストを作成
response = client.posts.create(
text="あなたの好きな色は何ですか?",
poll={"options": ["赤", "青", "緑", "黄色"], "duration_minutes": 1440}
)
print(f"Created poll: {response.data.id}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// 投票付きのポストを作成
const response = await client.posts.create({
text: "あなたの好きな色は何ですか?",
poll: { options: ["赤", "青", "緑", "黄色"], durationMinutes: 1440 },
});
console.log(`Created poll: ${response.data?.id}`);
ポストを削除する
1
ポストのIDを取得する
削除したいポストのIDが必要があります。これはポストの作成時に返されます。
2
DELETE リクエストを送信する
cURL
curl -X DELETE "https://api.x.com/2/tweets/1445880548472328192" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN"
from xdk import Client
from xdk.oauth1_auth import OAuth1
oauth1 = OAuth1(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET"
)
client = Client(auth=oauth1)
# ポストを削除する
response = client.posts.delete("1445880548472328192")
print(f"Deleted: {response.data.deleted}")
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
accessToken: "YOUR_ACCESS_TOKEN",
accessTokenSecret: "YOUR_ACCESS_TOKEN_SECRET",
});
const client = new Client({ oauth1 });
// ポストを削除する
const response = await client.posts.delete("1445880548472328192");
console.log(`Deleted: ${response.data?.deleted}`);
3
削除を確認する
{
"data": {
"deleted": true
}
}
削除できるのは、あなたが作成した投稿だけです。
次のステップ
連携ガイド
基本概念とベストプラクティス
メディアアップロード
投稿用メディアをアップロード
APIリファレンス
エンドポイントの完全なドキュメント
サンプルコード
実行可能なコード例