Install
Authenticate
Basic Example
Search tweets and write durable JSON Lines handoff rows:Workflow: Search Tweets to JSON Lines, CSV, or XLSX
This job is for Node.js, Bun, and queue workers that need searchable tweet data in a stable handoff format for queues, data lakes, analyst CSV files, or XLSX workbooks. It callsGET /x/tweets/search through client.x.tweets.search, uses the generated TweetSearchParams shape, and writes each returned tweet as one projected JSON object per line.
q
Maps to REST
q. Use it for the required X search query with keywords, handles, hashtags, or operators.limit
Maps to REST
limit. Use it as a 1 to 200 upper bound for a bounded pull. If page.has_next_page is true, keep the same q, filters, queryType, and limit when you continue with page.next_cursor.cursor
Maps to REST
cursor. Pass the opaque cursor from page.next_cursor to request the next page.sinceTime
Maps to REST
sinceTime. Use it as the ISO 8601 lower time bound.untilTime
Maps to REST
untilTime. Use it as the ISO 8601 upper time bound.queryType
Maps to REST
queryType. Use Latest for chronological results or Top for engagement-ranked results.Returned Data & Handoff
client.x.tweets.search returns PaginatedTweets:
page.tweets
JSON field
tweets. Contains tweet records with id, text, optional author, createdAt, likeCount, replyCount, retweetCount, quoteCount, bookmarkCount, viewCount, and isNoteTweet when available.page.has_next_page
TypeScript field
page.has_next_page. JSON field has_next_page. Tells your worker whether another page exists.page.next_cursor
JSON field
next_cursor. Store it only when page.has_next_page is true. For bounded pulls that return fewer tweets than limit, pass it back as cursor with the same query, filters, queryType, and limit.page.tweets into JSON Lines rows in xquik-tweet-search.jsonl for queues and data lakes, transform the same projected records into CSV for analysts, or produce XLSX from those rows when account teams need spreadsheets. Store tweet_id, author_username, engagement counts, page_index, page_cursor, next_cursor, and has_next_page so a worker can resume from the last saved cursor without replaying raw SDK objects. For explicit limit pulls, resume with the same query, filters, queryType, and limit; only cursor changes.
Workflow: Follower Export to CSV, JSON, or XLSX
Use this workflow when a Node.js, Bun, or queue worker needs an owned follower list for CRM import, warehouse loading, account scoring, analyst CSV, XLSX workbook delivery, or a resumable JSON handoff. It callsPOST /extractions/estimate through client.extractions.estimateCost, creates the job with client.extractions.run, reads saved rows with client.extractions.retrieve, and downloads files with client.extractions.exportResults.
client.extractions.run returns the queued 202 Accepted receipt from POST /extractions: id, toolType, and status: "running". Store job.id immediately, then poll client.extractions.retrieve before reading pages or calling client.extractions.exportResults. Credit reservation happens after the job starts. If available credits changed since estimateCost, the API can set resultsLimit to the affordable count before fetching rows or mark the job failed with insufficient_credits.follower_explorer requires targetUsername. Persist job.id, targetUsername, estimate.estimatedResults, and estimate.source before polling so a queue retry, worker restart, or agent handoff can resume the same export. client.extractions.retrieve returns results, hasMore, and nextCursor; pass nextCursor back as after when you need stored JSON pages before exporting files. Use xquik-followers.jsonl for queue replay or warehouse loads, xquik-followers.json for app ingestion, xquik-followers.csv for CRM import, and xquik-followers.xlsx for analyst handoff. Map exported User ID or row xUserId as the CRM unique key. Cost: 1 credit per follower extracted or returned. Exports are free after the extraction job exists.
Workflow: Tweet Replies to CSV, JSON, or XLSX
Use this workflow when a TypeScript worker needs every reply under one tweet as a saved extraction, JSON Lines handoff, or CSV/JSON/XLSX export. It usesclient.extractions.estimateCost, run, retrieve, and exportResults.
Reuse the polling, JSON Lines pagination, and export structure from the follower workflow. Only the tool type, target field, and filenames change:
reply_extractor requires targetTweetId. client.extractions.retrieve returns results, hasMore, and nextCursor; the shared pagination loop passes nextCursor back as after. Use xquik-replies.jsonl for queue replay or warehouse loads, xquik-replies.json for app ingestion, xquik-replies.csv for CRM import, and xquik-replies.xlsx for analyst handoff. client.extractions.exportResults supports csv, json, and xlsx for file handoff. Cost: 1 credit per reply extracted or returned.
Workflow: Post Media Tweets and DM Attachments
Use this workflow when a TypeScript worker, support queue, or agent service needs to post a media-backed tweet, reply with media, or send one uploaded media item in a DM. Tweet and reply media posts use public media URLs directly onclient.x.tweets.create. Send up to 4 image URLs or exactly 1 MP4 video URL up to 100 MB. Do not mix video with other media. Do not upload first when the media URL is already public.
tweetId, charged, and chargedCredits responses. The REST API can also return 202 x_write_unconfirmed; treat that runtime value as pending, store writeActionId and chargedCredits, and poll Get Write Action Status before sending another write.
reply_to_tweet_id:
media.mediaId as the only media_ids item:
client.x.tweets.create returns tweet.tweetId for confirmed posts or the pending write fields above when confirmation is still running. client.x.media.upload returns media.mediaId for DM attachments, and client.x.dm.send returns dm.messageId for support tickets, CRM records, queue jobs, or agent memory.
Keep DM body text in private systems. Shared logs, public artifacts, queue status, and agent handoffs should store message_id, optional media_id, account, user_id, and send status instead of full DM bodies. Leave reply_to_message_id unset even if generated SDK types expose it; the REST endpoint rejects DM reply threading.
Text-only tweet and reply writes cost 10 credits. Tweet media adds 2 credits per started MB across attached files. Uploading media costs 10 credits, and sending the DM costs 10 credits. Do not pass uploaded media.mediaId values to client.x.tweets.create; that method uses media with public media URLs.
Useful endpoints:
- Search Tweets
- Create Extraction
- Get Extraction
- Export Extraction
- Get User
- Create Tweet
- Upload Media
- Send Direct Message
- Create Monitor
- Create Webhook
Error Handling
The SDK throws typed errors for API failures:400 Bad Request
Throws
BadRequestError.401 Unauthenticated
Throws
AuthenticationError.403 Permission Denied
Throws
PermissionDeniedError.404 Not Found
Throws
NotFoundError.422 Unprocessable Entity
Throws
UnprocessableEntityError.429 Rate Limited
Throws
RateLimitError.5xx Server Error
Throws
InternalServerError.maxRetries to tune retry behavior.
Cost, Limits & Retries
Tweet search costs 1 credit per tweet returned. If remaining credits cannot cover a boundedlimit request, the API can return fewer tweets; if 0 paid results are affordable, it returns 402 insufficient_credits. Read calls are rate-limited, and 429 responses include Retry-After.
The client retries connection errors, 408, 409, 429, and 5xx responses by default. Handle RateLimitError with backoff, and fix 400, 401, 403, 404, or 422 responses before retrying.
Pagination
Search and list endpoints return page objects. Checkhas_next_page and pass the generated cursor fields documented on each endpoint when you need another page.