Working JavaScript code samples for the X (formerly Twitter) API v2.
node --version
No package.json is required. Examples use Node.js built-in modules or standard libraries.
For read-only operations (search, lookup):
export BEARER_TOKEN='your_bearer_token'
For user actions (post, like, repost, bookmark, mute, etc.):
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'
Note: Most user action examples (create post, like, repost, bookmark, mute, block, etc.) require OAuth 2.0 authentication with CLIENT_ID and CLIENT_SECRET. Read-only examples (search, lookup) only require BEARER_TOKEN.
posts/create_post.js- Create a new post (requiresCLIENT_ID,CLIENT_SECRET)posts/delete_post.js- Delete a post (requiresCLIENT_ID,CLIENT_SECRET)posts/get_liking_users.js- Get users who liked a post (requiresCLIENT_ID,CLIENT_SECRETfor user context)posts/get_post_counts_all.js- Get post counts (full archive)posts/get_post_counts_recent.js- Get post counts (recent)posts/get_posts_by_ids.js- Look up posts by ID (bearer token)posts/get_posts_by_ids_user_context.js- Look up posts by ID (user context) (requiresCLIENT_ID,CLIENT_SECRET)posts/get_quoted_posts.js- Get posts that quote a postposts/get_reposted_by.js- Get users who reposted a postposts/search_all.js- Full archive searchposts/search_recent.js- Recent search (last 7 days)
users/get_users_by_usernames.js- Look up users by username (bearer token)users/get_users_by_usernames_user_context.js- Look up users by username (user context) (requiresCLIENT_ID,CLIENT_SECRET)users/get_users_me.js- Get authenticated user (me) (requiresCLIENT_ID,CLIENT_SECRET)
users/block/get_blocking.js- Get users blocked by a user (requiresCLIENT_ID,CLIENT_SECRET)
users/bookmark/create_bookmark.js- Create a bookmark (requiresCLIENT_ID,CLIENT_SECRET)users/bookmark/delete_bookmark.js- Delete a bookmark (requiresCLIENT_ID,CLIENT_SECRET)users/bookmark/get_bookmarks.js- Get user's bookmarks (requiresCLIENT_ID,CLIENT_SECRET)
users/follow/get_followers.js- Get user's followersusers/follow/get_followers_paginated.js- Get user's followers (paginated)users/follow/get_following_paginated.js- Get users a user is following (paginated)
users/like/get_liked_posts.js- Get posts liked by a user (requiresCLIENT_ID,CLIENT_SECRET)users/like/like_post.js- Like a post (requiresCLIENT_ID,CLIENT_SECRET)users/like/unlike_post.js- Unlike a post (requiresCLIENT_ID,CLIENT_SECRET)
users/lists/follow_list.js- Follow a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/get_followed_lists.js- Get lists followed by a userusers/lists/get_list_memberships.js- Get list membershipsusers/lists/get_owned_lists.js- Get lists owned by a userusers/lists/pin_list.js- Pin a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/unfollow_list.js- Unfollow a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/unpin_list.js- Unpin a list (requiresCLIENT_ID,CLIENT_SECRET)
users/mute/get_muting.js- Get users muted by a user (requiresCLIENT_ID,CLIENT_SECRET)users/mute/mute_user.js- Mute a user (requiresCLIENT_ID,CLIENT_SECRET)users/mute/unmute_user.js- Unmute a user (requiresCLIENT_ID,CLIENT_SECRET)
users/repost/repost_post.js- Repost a post (requiresCLIENT_ID,CLIENT_SECRET)users/repost/unrepost_post.js- Undo a repost (requiresCLIENT_ID,CLIENT_SECRET)
users/timeline/get_home_timeline.js- Get home timeline (requiresCLIENT_ID,CLIENT_SECRET)users/timeline/get_mentions.js- Get user mentions timelineusers/timeline/get_posts.js- Get user's posts timelineusers/timeline/get_posts_paginated.js- Get user's posts timeline (paginated)
- See
users/timeline/directory for timeline examples
streams/stream_posts_filtered.js- Filtered stream with rulesstreams/stream_posts_sample.js- Sampled stream
lists/add_member.js- Add member to a list (requiresCLIENT_ID,CLIENT_SECRET)lists/create_list.js- Create a new list (requiresCLIENT_ID,CLIENT_SECRET)lists/delete_list.js- Delete a list (requiresCLIENT_ID,CLIENT_SECRET)lists/get_list_by_id.js- Get 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 member from a list (requiresCLIENT_ID,CLIENT_SECRET)lists/update_list.js- Update a list (requiresCLIENT_ID,CLIENT_SECRET)
spaces/get_spaces_by_ids.js- Look up Spaces by IDspaces/search_spaces.js- Search for Spaces
compliance/create_jobs.js- Create compliance jobcompliance/download_results.js- Download compliance resultscompliance/get_jobs_by_id.js- Get compliance job by IDcompliance/get_jobs.js- Get compliance jobscompliance/upload_ids.js- Upload IDs for compliance
usage/get_usage.js- Get API usage information
# Make sure environment variables are set
node posts/search_recent.js