PrerequisitesBefore you begin, you’ll need:
- A developer account with an approved App
- User Access Tokens (OAuth 1.0a or OAuth 2.0 PKCE)
Create a Post
Prepare your request
The POST
/2/tweets endpoint requires a JSON body with at least text or media:{
"text": "Hello from the X API!"
}
Send the request
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)
# Create a Post
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 });
// Create a Post
const response = await client.posts.create({ text: "Hello from the X API!" });
console.log(`Created Post: ${response.data?.id}`);
Advanced examples
Reply to a Post
Reply to a Post
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)
# Create a reply
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 });
// Create a reply
const response = await client.posts.create({
text: "This is a reply!",
reply: { inReplyToTweetId: "1234567890" },
});
console.log(`Created reply: ${response.data?.id}`);
Quote a Post
Quote a Post
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)
# Quote a Post
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 });
// Quote a Post
const response = await client.posts.create({
text: "Check this out!",
quoteTweetId: "1234567890",
});
console.log(`Created quote: ${response.data?.id}`);
Post with media
Post with media
First, upload media using the Media Upload endpoint, then reference the
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)
# Post with media
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 });
// Post with media
const response = await client.posts.create({
text: "Photo of the day!",
media: { mediaIds: ["1234567890123456789"] },
});
console.log(`Created Post with media: ${response.data?.id}`);
Post with poll
Post with poll
cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "What is your favorite color?",
"poll": {
"options": ["Red", "Blue", "Green", "Yellow"],
"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)
# Post with poll
response = client.posts.create(
text="What is your favorite color?",
poll={"options": ["Red", "Blue", "Green", "Yellow"], "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 });
// Post with poll
const response = await client.posts.create({
text: "What is your favorite color?",
poll: { options: ["Red", "Blue", "Green", "Yellow"], durationMinutes: 1440 },
});
console.log(`Created poll: ${response.data?.id}`);
Post with paid partnership
Post with paid partnership
Use the
paid_partnership field to indicate that this Post is a paid partnership (i.e., the author is disclosing it contains paid promotion). When set to true, the Post will be labeled as a paid promotion.cURL
curl -X POST "https://api.x.com/2/tweets" \
-H "Authorization: Bearer $USER_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "Excited to partner with Acme on their latest launch!",
"paid_partnership": true
}'
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)
# Post with paid partnership
response = client.posts.create(
text="Excited to partner with Acme on their latest launch!",
paid_partnership=True
)
print(f"Created paid partnership 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 });
// Post with paid partnership
const response = await client.posts.create({
text: "Excited to partner with Acme on their latest launch!",
paidPartnership: true,
});
console.log(`Created paid partnership post: ${response.data?.id}`);
Delete a Post
Get the Post ID
You need the ID of the Post you want to delete. This is returned when you create a Post.
Send a DELETE request
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)
# Delete a Post
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 });
// Delete a Post
const response = await client.posts.delete("1445880548472328192");
console.log(`Deleted: ${response.data?.deleted}`);
You can only delete Posts that you authored.
Next steps
Integration guide
Key concepts and best practices
Media upload
Upload media for Posts
API Reference
Full endpoint documentation
Sample code
Working code examples