Skip to content

Repository files navigation

TwexAPI TypeScript SDK: Twitter API for search, followers, DMs, communities & X automation

Use the TwexAPI TypeScript SDK to search tweets, scrape Twitter followers, and read X profiles, timelines, replies, and threads. Send DMs, search communities, fetch lists, articles, hashtags, cashtags, and global trending tweets with generated types and agent Skills. Like, retweet, follow, and post through documented REST routes. It is a Twitter API alternative for apps, scripts, and MCP clients.

API Map | REST API | MCP Guide | Dashboard

Speakeasy generates this SDK.

Pi coding agent package

Install the bundled TwexAPI Skills directly from npm:

pi install npm:@twexapi-dev/x-api-scraper

Pi loads the packaged Skills from skills/:

  • x-api-scraper — routing, safety, SDK, and reference files
  • x-api-scraper-research — bounded public research reads

Import the typed SDK from the same npm package.

Common Twitter & X tasks

Task REST Route Usage
Search tweets without the X API POST /twitter/advanced_search/page Use keyword queries and paginate with a cursor.
Search hashtags or cashtags POST /twitter/hashtags, POST /twitter/cashtags Filter by tag and sort order.
Read an X profile GET /twitter/{screen_name}/about Look up a user by screen name.
Read a profile timeline POST /twitter/{screen_name}/timeline/page Paginate bounded results.
Scrape Twitter followers POST /v3/twitter/users/followers Use the v3 follower list.
Scrape following accounts POST /v3/twitter/users/following Use the v3 following list.
Read tweet replies POST /twitter/tweets/{tweet_id}/replies/page Paginate replies by tweet id.
Read a tweet thread POST /twitter/tweets/thread_by_id Fetch the thread from a root tweet.
Send or read DMs /v3/twitter/send-dm, /v3/twitter/dm-history Use v3 XChat endpoints.
Search communities POST /twitter/community/search Find communities, then load tweets or members.
Get global trending tweets GET /twitter/global-trending/tweets Filter by country, topic, and content.
Post or reply POST /twitter/tweets/create Confirm the account cookie and payload.

See api.md for the complete API.

AI agent workflows with MCP

Use the typed REST SDK in application code. Add https://api.twexapi.io/mcp to MCP clients. Follow the MCP guide for current authentication support.

Package & registry trust

Installation

Requires a JavaScript runtime with ECMAScript 2020 and fetch. See RUNTIMES.md.

npm install @twexapi-dev/x-api-scraper

pnpm, bun, and yarn also work.

Usage

See api.md for the complete API.

Get an API key from the TwexAPI dashboard. Pass it as bearerAuth, or set X_API_SCRAPER_KEY.

import { XApiScraper } from "@twexapi-dev/x-api-scraper";

const client = new XApiScraper({
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

const result = await client.search.advanced({
  searchTerms: ["from:elonmusk"],
  sortBy: "Latest",
  nextCursor: "",
});

Look up a profile and paginate followers:

const about = await client.users.getAbout({ screenName: "elonmusk" });

const followers = await client.users.followers.list({
  screenName: "elonmusk",
});

Keep API keys out of source code, URLs, and logs.

Authentication

This SDK uses HTTP Bearer authentication. Set bearerAuth when creating the client.

Write actions (tweet, follow, like, DM send) also need a Twitter cookie or auth_token on the request. Pass them on the operation input.

Request & response types

The package includes types for every request parameter and response field. Import them directly:

import { XApiScraper } from "@twexapi-dev/x-api-scraper";
import type {
  AdvancedSearchCursorQuery,
  AdvancedSearchCursorResponse,
} from "@twexapi-dev/x-api-scraper/models";

const client = new XApiScraper({
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

const params: AdvancedSearchCursorQuery = {
  searchTerms: ["from:elonmusk"],
  sortBy: "Latest",
  nextCursor: "",
};
const result: AdvancedSearchCursorResponse = await client.search.advanced(params);

Editors show each method, parameter, and field description from its docstring.

Available Resources and Operations

Available methods

  • fetch - Batch Fetch X Articles
  • markdown - Fetch Article as Markdown
  • page - Get Replies by Page
  • list - Get Followers (v3)
  • verified - Get Verified Followers
  • list - Get Following (v3)

Standalone functions

All of the methods above are also exported as standalone functions for tree-shaking. See FUNCTIONS.md.

Handling errors

XAPIScraperError is the base class for HTTP error responses.

import { XApiScraper } from "@twexapi-dev/x-api-scraper";
import * as errors from "@twexapi-dev/x-api-scraper/models/errors";

const client = new XApiScraper({
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

try {
  await client.search.advanced({
    searchTerms: ["from:elonmusk"],
    sortBy: "Latest",
    nextCursor: "",
  });
} catch (error) {
  if (error instanceof errors.XAPIScraperError) {
    console.log(error.statusCode);
    console.log(error.body);
  } else {
    throw error;
  }
}
Property Type Description
error.message string Error message
error.statusCode number HTTP status code
error.headers Headers Response headers
error.body string Response body
error.rawResponse Response Raw fetch response

Network errors include ConnectionError, RequestTimeoutError, and RequestAbortedError. Validation failures may throw HTTPValidationError (422).

Retries

Some operations support retries. The SDK uses exponential backoff by default.

Override retries per request:

import { XApiScraper } from "@twexapi-dev/x-api-scraper";

const client = new XApiScraper({
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

const result = await client.search.advanced(
  {
    searchTerms: ["from:elonmusk"],
    sortBy: "Latest",
    nextCursor: "",
  },
  {
    retries: {
      strategy: "backoff",
      backoff: {
        initialInterval: 1,
        maxInterval: 50,
        exponent: 1.1,
        maxElapsedTime: 100,
      },
      retryConnectionErrors: false,
    },
  },
);

Or set retryConfig on the client for every operation that supports retries.

Timeouts

Set timeoutMs on the client or on one request. Timed-out requests throw RequestTimeoutError.

const client = new XApiScraper({
  timeoutMs: 20 * 1000,
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

await client.search.advanced(
  {
    searchTerms: ["from:elonmusk"],
    sortBy: "Latest",
    nextCursor: "",
  },
  {
    timeoutMs: 5 * 1000,
  },
);

Server selection

The default server is https://api.twexapi.io. Override it with server: "production" or serverURL.

const client = new XApiScraper({
  serverURL: "https://api.twexapi.io",
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

Logging

Warning

Debug logs can include API tokens. Use this only during local development.

Pass debugLogger: console to log requests and responses.

const client = new XApiScraper({
  debugLogger: console,
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

Custom HTTP client

The SDK uses the global fetch function by default.

Polyfill the global to use another fetch implementation:

import fetch from "my-fetch";

globalThis.fetch = fetch;

Or pass an HTTPClient with a custom fetcher:

import { XApiScraper } from "@twexapi-dev/x-api-scraper";
import { HTTPClient } from "@twexapi-dev/x-api-scraper/lib/http";
import fetch from "my-fetch";

const httpClient = new HTTPClient({ fetcher: fetch });
const client = new XApiScraper({
  httpClient,
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

Fetch options

Pass RequestInit fields on a request without replacing fetch. Request options take precedence.

await client.search.advanced(
  {
    searchTerms: ["from:elonmusk"],
    sortBy: "Latest",
    nextCursor: "",
  },
  {
    headers: {
      "X-Custom-Header": "value",
    },
  },
);

Proxies

Add runtime-specific proxy settings through a custom HTTPClient fetcher.

Node [docs]

import { XApiScraper } from "@twexapi-dev/x-api-scraper";
import { HTTPClient } from "@twexapi-dev/x-api-scraper/lib/http";
import * as undici from "undici";

const proxyAgent = new undici.ProxyAgent("http://localhost:8888");
const httpClient = new HTTPClient({
  fetcher: (input, init) =>
    fetch(input, { ...init, dispatcher: proxyAgent } as RequestInit),
});

const client = new XApiScraper({
  httpClient,
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

Bun [docs]

import { XApiScraper } from "@twexapi-dev/x-api-scraper";
import { HTTPClient } from "@twexapi-dev/x-api-scraper/lib/http";

const httpClient = new HTTPClient({
  fetcher: (input, init) =>
    fetch(input, { ...init, proxy: "http://localhost:8888" } as RequestInit),
});

const client = new XApiScraper({
  httpClient,
  bearerAuth: process.env.X_API_SCRAPER_KEY,
});

Deno [docs]

import { XApiScraper } from "npm:@twexapi-dev/x-api-scraper";
import { HTTPClient } from "npm:@twexapi-dev/x-api-scraper/lib/http";

const denoHttp = Deno.createHttpClient({
  proxy: { url: "http://localhost:8888" },
});
const httpClient = new HTTPClient({
  fetcher: (input, init) =>
    fetch(input, { ...init, client: denoHttp } as RequestInit),
});

const client = new XApiScraper({
  httpClient,
  bearerAuth: Deno.env.get("X_API_SCRAPER_KEY"),
});

Semantic versioning

This package follows SemVer with these exceptions:

  1. Static type changes that preserve runtime behavior.
  2. Changes to undocumented internals that remain technically public.
  3. Changes unlikely to affect normal use.

Open an issue with questions, bugs, or suggestions.

Runtime support

Supports these runtimes:

  • Current Chrome, Firefox, Safari, Edge, and other web browsers.
  • Maintained Node.js 18 LTS or later.
  • Deno v1.39 or higher.
  • Bun 1.0 or later.
  • Cloudflare Workers.
  • Vercel Edge Runtime.

See RUNTIMES.md for compiler options and runtime notes.

React Native is not supported.

Request another runtime in a GitHub issue.

Contributing

This repository contains generated code. See CONTRIBUTING.md.

To regenerate the Speakeasy input spec:

node --test tests/build-openapi-sdk.test.mjs
node scripts/build-openapi-sdk.mjs openapi.source.json openapi.sdk.json docs/openapi-prep-report.json docs/openapi-prep-report.md

TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp.

About

TypeScript SDK for Twitter search, followers, DMs, communities & X automation. Built for TwexAPI. Not affiliated with X Corp.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages