게시물 생성하기
1
요청 준비하기
POST
/2/tweets 엔드포인트를 호출하려면 JSON 본문에 최소한 text 또는 media 필드가 포함되어 있어야 합니다.{
"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": "This is a reply!",
"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="This is a reply!",
reply={"in_reply_to_tweet_id": "1234567890"}
)
print(f"Created reply: {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: "This is a reply!",
reply: { inReplyToTweetId: "1234567890" },
});
console.log(`Created reply: ${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": "Check this out!",
"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="Check this out!",
quote_tweet_id="1234567890"
)
print(f"Created quote: {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: "Check this out!",
quoteTweetId: "1234567890",
});
console.log(`Created quote: ${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": "오늘의 사진!",
"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="오늘의 사진!",
media={"media_ids": ["1234567890123456789"]}
)
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: "오늘의 사진!",
media: { mediaIds: ["1234567890123456789"] },
});
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": "가장 좋아하는 색은 무엇인가요?",
"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 참조 문서
엔드포인트 전체 문서
샘플 코드
실행 가능한 코드 예제