Official TypeScript (Node.js 18+ ESM) sample code for the X API v2 powered by XDK for TypeScript (@xdevplatform/xdk).
All 84 samples are strictly type-checked (tsc --noEmit with "strict": true).
- Node.js 18+ (
node --version) - An X Developer Account with API credentials
cd typescript
npm installFor App-only read operations (search, lookup, streams, webhooks, activity):
export BEARER_TOKEN="your_bearer_token"For User-context actions (create post, like, repost, bookmark, mute, follow, DMs, media upload):
export CLIENT_ID="your_oauth2_client_id"
export CLIENT_SECRET="your_oauth2_client_secret" # optional for public clients
export REDIRECT_URI="https://example.com" # must match your App settings(You can also set ACCESS_TOKEN directly if you already have a valid OAuth 2.0 user token.)
cd typescript
npm install
# Run any sample directly via tsx
npx tsx posts/search_recent.ts
# Strictly type-check all 84 samples
npm run typecheckactivity/create_subscription.ts- Subscribe to real-time X Activity eventsactivity/delete_subscription.ts- Delete an Activity subscriptionactivity/list_subscriptions.ts- List active Activity subscriptionsactivity/stream_events.ts- Stream real-time Activity eventsactivity/update_subscription.ts- Update an Activity subscription tag / webhook
compliance/create_jobs.ts- Create a batch compliance jobcompliance/download_results.ts- Download compliance resultscompliance/get_jobs.ts- List compliance jobscompliance/get_jobs_by_id.ts- Get compliance job status by IDcompliance/upload_ids.ts- Upload Post/User IDs to a compliance job
direct_messages/get_events_by_conversation.ts- Fetch DM events in a conversationdirect_messages/get_one_to_one_conversation_events.ts- Fetch 1-to-1 DM events with a participantdirect_messages/get_user_conversation_events.ts- Fetch recent DM events across conversationsdirect_messages/post_dm_to_conversation.ts- Send a message to an existing conversationdirect_messages/post_group_conversation_dm.ts- Create a new group DM conversationdirect_messages/post_one_to_one_dm.ts- Send a 1-to-1 Direct Message
lists/add_member.ts- Add a member to a Listlists/create_list.ts- Create a new Listlists/delete_list.ts- Delete a Listlists/get_list_by_id.ts- Look up a List by IDlists/get_list_followers.ts- Get List followerslists/get_list_members.ts- Get List memberslists/get_list_posts.ts- Get Posts from a Listlists/remove_member.ts- Remove a member from a Listlists/update_list.ts- Update a List
media/upload.ts- Single-payload image upload (POST /2/media/upload)media/media_upload_v2.ts- Chunked video upload (initialize->append->finalize->STATUS->posts.create)
posts/create_post.ts- Create a Postposts/delete_post.ts- Delete a Postposts/get_liking_users.ts- Get users who liked a Postposts/get_post_by_id.ts- Look up a single Post by IDposts/get_post_counts_all.ts- Full-archive Post countsposts/get_post_counts_recent.ts- Recent Post counts (last 7 days)posts/get_posts_by_ids.ts- Look up multiple Posts by IDs (Bearer Token)posts/get_posts_by_ids_user_context.ts- Look up multiple Posts by IDs (OAuth 2.0 User Context)posts/get_quoted_posts.ts- Get Quote Posts for a Postposts/get_reposted_by.ts- Get users who reposted a Postposts/hide_reply.ts- Hide or unhide a replyposts/search_all.ts- Full-archive searchposts/search_recent.ts- Recent search (last 7 days)
spaces/get_spaces_by_ids.ts- Look up Spaces by IDsspaces/search_spaces.ts- Search live/scheduled Spaces
streams/stream_posts_filtered.ts- Filtered stream with rule managementstreams/stream_posts_sample.ts- 1% sampled volume stream
usage/get_usage.ts- Daily API Post usage metrics
users/get_users_by_usernames.ts,users/get_users_by_usernames_user_context.ts,users/get_users_me.tsusers/block/get_blocking.tsusers/bookmark/{create_bookmark,delete_bookmark,get_bookmarks}.tsusers/follow/{follow_user,get_followers,get_followers_paginated,get_following,get_following_paginated,unfollow_user}.tsusers/like/{get_liked_posts,like_post,unlike_post}.tsusers/lists/{follow_list,get_followed_lists,get_list_memberships,get_owned_lists,get_pinned_lists,pin_list,unfollow_list,unpin_list}.tsusers/mute/{get_muting,mute_user,unmute_user}.tsusers/repost/{get_reposts_of_me,repost_post,unrepost_post}.tsusers/timeline/{get_home_timeline,get_mentions,get_posts,get_posts_paginated}.ts
webhooks/register_webhook.ts- Register a webhook URLwebhooks/list_webhooks.ts- List registered webhookswebhooks/validate_webhook.ts- Trigger a CRC validation checkwebhooks/delete_webhook.ts- Delete a webhookwebhooks/webhook_server.ts- Minimal Node.js HTTP server handling CRC (HMAC-SHA256) and POST deliveries