> ## Documentation Index
> Fetch the complete documentation index at: https://docs.twexapi.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Ruby SDK

> Use the TwexAPI Ruby SDK for tweet search, follower exports, DMs, and X automation in Ruby apps and scripts.

Use the Ruby SDK when you want typed request objects, Faraday-backed HTTP, retries, and pagination helpers for TwexAPI REST workflows in Ruby apps and scripts.

| Ruby task      | SDK call                      | Save          |
| -------------- | ----------------------------- | ------------- |
| Search tweets  | `client.search.advanced`      | `next_cursor` |
| Read profile   | `client.users.get_about`      | `screen_name` |
| List followers | `client.users.followers.list` | cursor        |

## Install

Add the gem to your `Gemfile`:

```ruby theme={null}
gem "x_api_scraper", "~> 0.1.0"
```

Or install from GitHub until the gem is published:

```ruby theme={null}
gem "x_api_scraper", git: "https://github.com/twexapi-dev/x-api-scraper-ruby.git"
```

Source: [twexapi-dev/x-api-scraper-ruby](https://github.com/twexapi-dev/x-api-scraper-ruby)

## Authenticate

```bash theme={null}
export X_API_SCRAPER_KEY="YOUR_API_KEY"
```

```ruby theme={null}
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)
```

## Basic example

Look up a profile and list followers:

```ruby theme={null}
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")
)
```

## Workflow: search tweets

```ruby theme={null}
cursor = ""
page_index = 0

loop do
  req = Models::Components::AdvancedSearchCursorQuery.new(
    search_terms: ["from:elonmusk AI"],
    sort_by: "Latest",
    next_cursor: cursor
  )
  page = client.search.advanced(request: req).advanced_search_cursor_response

  page.tweets&.each do |tweet|
    row = {
      source: "twexapi.ruby.search",
      tweet_id: tweet.id,
      text: tweet.text,
      page_index: page_index,
      next_cursor: page.next_cursor
    }
    puts row.to_json
  end

  break unless page.has_next_page && page.next_cursor && !page.next_cursor.empty?

  cursor = page.next_cursor
  page_index += 1
end
```

## Workflow: post a tweet

Write actions need a Twitter cookie or `auth_token` on the request:

```ruby theme={null}
client.tweets.actions.create(
  request: Models::Components::CreateTweetRequest.new(
    tweet_content: "Hello from the TwexAPI Ruby SDK.",
    cookie: ENV["TWITTER_COOKIE"]
  )
)
```

## Error handling

```ruby theme={null}
begin
  client.search.advanced(request: req)
rescue Models::Errors::HTTPValidationError => e
  raise e
rescue Errors::APIError => e
  puts e.status_code, e.body
end
```

## References

* [SDKs overview](/sdks)
* [Authentication](/authentication)
* [Advanced Twitter Search](/api-reference/search-endpoints/advanced-twitter-search)
* [Source Repository](https://github.com/twexapi-dev/x-api-scraper-ruby)
