Official JavaScript (Node.js 18+) sample code for the X API v2 powered by XDK for TypeScript / JavaScript (@xdevplatform/xdk).
- Node.js 18+ (
node --version) - An X Developer Account with API credentials
cd javascript
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.)
activity/create_subscription.js- Subscribe to real-time X Activity eventsactivity/delete_subscription.js- Delete an Activity subscriptionactivity/list_subscriptions.js- List active Activity subscriptionsactivity/stream_events.js- Stream real-time Activity eventsactivity/update_subscription.js- Update an Activity subscription tag / webhook
compliance/create_jobs.js- Create a batch compliance jobcompliance/download_results.js- Download compliance resultscompliance/get_jobs.js- List compliance jobscompliance/get_jobs_by_id.js- Get compliance job status by IDcompliance/upload_ids.js- Upload Post/User IDs to a compliance job
direct_messages/get_events_by_conversation.js- Fetch DM events in a conversationdirect_messages/get_one_to_one_conversation_events.js- Fetch 1-to-1 DM events with a participantdirect_messages/get_user_conversation_events.js- Fetch recent DM events across conversationsdirect_messages/post_dm_to_conversation.js- Send a message to an existing conversationdirect_messages/post_group_conversation_dm.js- Create a new group DM conversationdirect_messages/post_one_to_one_dm.js- Send a 1-to-1 Direct Message
lists/add_member.js- Add a member to a Listlists/create_list.js- Create a new Listlists/delete_list.js- Delete a Listlists/get_list_by_id.js- Look up a List by IDlists/get_list_followers.js- Get List followerslists/get_list_members.js- Get List memberslists/get_list_posts.js- Get Posts from a Listlists/remove_member.js- Remove a member from a Listlists/update_list.js- Update a List
media/upload.js- Single-payload image upload (POST /2/media/upload)media/media_upload_v2.js- Chunked video upload (initialize->append->finalize->STATUS->posts.create)
posts/create_post.js- Create a Postposts/delete_post.js- Delete a Postposts/get_liking_users.js- Get users who liked a Postposts/get_post_by_id.js- Look up a single Post by IDposts/get_post_counts_all.js- Full-archive Post countsposts/get_post_counts_recent.js- Recent Post counts (last 7 days)posts/get_posts_by_ids.js- Look up multiple Posts by IDs (Bearer Token)posts/get_posts_by_ids_user_context.js- Look up multiple Posts by IDs (OAuth 2.0 User Context)posts/get_quoted_posts.js- Get Quote Posts for a Postposts/get_reposted_by.js- Get users who reposted a Postposts/hide_reply.js- Hide or unhide a replyposts/search_all.js- Full-archive searchposts/search_recent.js- Recent search (last 7 days)
spaces/get_spaces_by_ids.js- Look up Spaces by IDsspaces/search_spaces.js- Search live/scheduled Spaces
streams/stream_posts_filtered.js- Filtered stream with rule managementstreams/stream_posts_sample.js- 1% sampled volume stream
usage/get_usage.js- Daily API Post usage metrics
users/get_users_by_usernames.js,users/get_users_by_usernames_user_context.js,users/get_users_me.jsusers/block/get_blocking.jsusers/bookmark/{create_bookmark,delete_bookmark,get_bookmarks}.jsusers/follow/{follow_user,get_followers,get_followers_paginated,get_following,get_following_paginated,unfollow_user}.jsusers/like/{get_liked_posts,like_post,unlike_post}.jsusers/lists/{follow_list,get_followed_lists,get_list_memberships,get_owned_lists,get_pinned_lists,pin_list,unfollow_list,unpin_list}.jsusers/mute/{get_muting,mute_user,unmute_user}.jsusers/repost/{get_reposts_of_me,repost_post,unrepost_post}.jsusers/timeline/{get_home_timeline,get_mentions,get_posts,get_posts_paginated}.js
webhooks/register_webhook.js- Register a webhook URLwebhooks/list_webhooks.js- List registered webhookswebhooks/validate_webhook.js- Trigger a CRC validation checkwebhooks/delete_webhook.js- Delete a webhookwebhooks/webhook_server.js- Minimal Node.js HTTP server handling CRC (HMAC-SHA256) and POST deliveries
cd javascript
npm install
node posts/search_recent.js