Requisitos previosAntes de comenzar, necesitarás:
- Una cuenta de desarrollador con una App aprobada
- User Access Tokens (OAuth 1.0a o OAuth 2.0 PKCE)
Crear un Post
1
Prepara tu solicitud
El endpoint POST
/2/tweets requiere un cuerpo JSON con al menos text o media:{
"text": "Hello from the X API!"
}
2
Envía la solicitud
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)
# Crea un 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 });
// Crea un Post
const response = await client.posts.create({ text: "Hello from the X API!" });
console.log(`Created Post: ${response.data?.id}`);
3
Revisa la respuesta
Una respuesta exitosa incluye el
id y text del nuevo Post:{
"data": {
"id": "1445880548472328192",
"text": "Hello from the X API!"
}
}
Ejemplos avanzados
Responder a un Post
Responder a un 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)
# Crea una respuesta
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 });
// Crea una respuesta
const response = await client.posts.create({
text: "This is a reply!",
reply: { inReplyToTweetId: "1234567890" },
});
console.log(`Created reply: ${response.data?.id}`);
Citar un Post
Citar un 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)
# Cita un 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 });
// Cita un Post
const response = await client.posts.create({
text: "Check this out!",
quoteTweetId: "1234567890",
});
console.log(`Created quote: ${response.data?.id}`);
Post con media
Post con media
Primero, sube el media usando el endpoint de Media Upload, luego haz referencia al
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 con 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 con 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 con encuesta
Post con encuesta
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 con encuesta
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 con encuesta
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 con paid partnership
Post con paid partnership
Utiliza el campo
paid_partnership para indicar que este Post es una asociación pagada (es decir, el autor está declarando que contiene una promoción pagada). Cuando se establece en true, el Post será etiquetado como una promoción pagada.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 con 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 con 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}`);
Eliminar un Post
1
Obtén el ID del Post
Necesitas el ID del Post que quieres eliminar. Este se devuelve cuando creas un Post.
2
Envía una solicitud 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)
# Elimina un 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 });
// Elimina un Post
const response = await client.posts.delete("1445880548472328192");
console.log(`Deleted: ${response.data?.deleted}`);
3
Confirma la eliminación
{
"data": {
"deleted": true
}
}
Solo puedes eliminar Posts que hayas creado tú.
Próximos pasos
Guía de integración
Conceptos clave y mejores prácticas
Subida de media
Sube media para Posts
Referencia de la API
Documentación completa del endpoint
Código de ejemplo
Ejemplos de código funcional