Skip to content

Latest commit

 

History

History
506 lines (379 loc) · 18.7 KB

File metadata and controls

506 lines (379 loc) · 18.7 KB

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

Use the TwexAPI Ruby 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 typed request objects. Like, retweet, follow, and post through documented REST routes. It is a Twitter API alternative for apps and scripts.

REST API | TypeScript SDK | Dashboard

Speakeasy generates this SDK.

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 GET /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.

Package & registry trust

Summary

X API Scraper: Speakeasy-ready OpenAPI document for the x-api-scraper TypeScript SDK.

The SDK wraps TwexAPI's X/Twitter API surface with bearer-token authentication. Cookie/proxy based endpoints are excluded except tweet actions, follow/unfollow, and v3 DM operations. Paid engagement services, profile mutation, legacy-only operations, and non-v3 follower/following endpoints remain excluded.

Not affiliated with X Corp.

Table of Contents

SDK Installation

Requires Ruby 3.2.0 or higher.

Add the gem to your Gemfile:

gem "x_api_scraper", "~> 0.1.0"

Or install from GitHub until the gem is published:

gem "x_api_scraper", git: "https://github.com/twexapi-dev/x-api-scraper-ruby.git"

SDK Example Usage

Example

require "x_api_scraper"

Models = ::XapiScraper::Models
client = ::XapiScraper::Client.new(
  bearer_auth: ENV["X_API_SCRAPER_KEY"]
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: ["from:elonmusk"],
  sort_by: "Latest",
  next_cursor: ""
)
res = client.search.advanced(request: req)
puts(res.advanced_search_cursor_response)

about = client.users.get_about(
  request: Models::Operations::UsersGetAboutRequest.new(screen_name: "elonmusk")
)
followers = client.users.followers.list(
  request: Models::Components::FollowersFollowingV3Query.new(username: "elonmusk")
)

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

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

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

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
bearer_auth http HTTP Bearer

To authenticate with the API the bearer_auth parameter must be set when initializing the SDK client instance. For example:

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: [
    "<value 1>",
    "<value 2>",
    "<value 3>"
  ],
  sort_by: "<value>",
  next_cursor: ""
)
res = s.search.advanced(request: req)

unless res.advanced_search_cursor_response.nil?
  # handle response
end

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)

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a RetryConfig object to the call:

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: [
    "<value 1>",
    "<value 2>",
    "<value 3>"
  ],
  sort_by: "<value>",
  next_cursor: ""
)
res = s.search.advanced(request: req)

unless res.advanced_search_cursor_response.nil?
  # handle response
end

If you'd like to override the default retry strategy for all operations that support retries, you can use the retry_config optional parameter when initializing the SDK:

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  retry_config: Utils::RetryConfig.new(
    backoff: Utils::BackoffStrategy.new(
      exponent: 1.1,
      initial_interval: 1,
      max_elapsed_time: 100,
      max_interval: 50
    ),
    retry_connection_errors: false,
    strategy: "backoff"
  ),
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: [
    "<value 1>",
    "<value 2>",
    "<value 3>"
  ],
  sort_by: "<value>",
  next_cursor: ""
)
res = s.search.advanced(request: req)

unless res.advanced_search_cursor_response.nil?
  # handle response
end

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an error.

By default an API error will raise a Errors::APIError, which has the following properties:

Property Type Description
message string The error message
status_code int The HTTP status code
raw_response Faraday::Response The raw HTTP response
body string The response content

When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the advanced method throws the following exceptions:

Error Type Status Code Content Type
Models::Errors::HTTPValidationError 422 application/json
Errors::APIError 4XX, 5XX */*

Example

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

begin
  req = Models::Components::AdvancedSearchCursorQuery.new(
    search_terms: [
      "<value 1>",
      "<value 2>",
      "<value 3>"
    ],
    sort_by: "<value>",
    next_cursor: ""
  )
  res = s.search.advanced(request: req)

  unless res.advanced_search_cursor_response.nil?
    # handle response
  end

rescue Models::Errors::HTTPValidationError => e
  # handle e.container data
  raise e
rescue Errors::APIError => e
  # handle default exception
  raise e
end

Server Selection

Select Server by Name

You can override the default server globally by passing a server name to the server (Symbol) optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:

Name Server Description
production https://api.twexapi.io TwexAPI production API

Example

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  server: "production",
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: [
    "<value 1>",
    "<value 2>",
    "<value 3>"
  ],
  sort_by: "<value>",
  next_cursor: ""
)
res = s.search.advanced(request: req)

unless res.advanced_search_cursor_response.nil?
  # handle response
end

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the server_url (String) optional parameter when initializing the SDK client instance. For example:

require "x_api_scraper"

Models = ::XapiScraper::Models
s = ::XapiScraper::Client.new(
  server_url: "https://api.twexapi.io",
  bearer_auth: "<YOUR_BEARER_TOKEN_HERE>"
)

req = Models::Components::AdvancedSearchCursorQuery.new(
  search_terms: [
    "<value 1>",
    "<value 2>",
    "<value 3>"
  ],
  sort_by: "<value>",
  next_cursor: ""
)
res = s.search.advanced(request: req)

unless res.advanced_search_cursor_response.nil?
  # handle response
end

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

To regenerate from the OpenAPI document:

speakeasy run

The source spec lives in openapi.sdk.json. Keep it in sync with x-api-scraper-typescript.

GitHub Actions can regenerate the SDK and publish the gem. Add these repository secrets first:

  • SPEAKEASY_API_KEY
  • RUBYGEMS_AUTH_TOKEN

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

SDK Created by Speakeasy