Skip to content
xdevplatformPublic

About

Official Ruby SDK for the X API v2

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

XDK for Ruby

Official Ruby SDK for the X API v2 (v0.1.0).

  • Zero runtime dependencies (uses Ruby 3.0+ standard library net/http, openssl, json)
  • Full X API v2 coverage: Posts, Users, Chat, Media, Lists, Direct Messages, Streaming, Activity, Trends, Communities, and more
  • Authentication: Bearer Token (app-only), OAuth 2.0 PKCE (with automatic token refresh), and OAuth 1.0a (HMAC-SHA1)
  • Pagination: Lazy Ruby Enumerator over next_token / pagination_token
  • Streaming: Real-time NDJSON streaming with keep-alive callbacks, idle-timeout recovery, and exponential backoff

Installation

gem install xdk

Or add to your Gemfile:

gem "xdk"

Requires Ruby 3.0+.

Quick Start

Bearer Token (App-Only)

require "xdk"

client = Xdk::Client.new(bearer_token: ENV.fetch("X_BEARER_TOKEN"))
user = client.users.get_by_username(username: "XDevelopers")
puts user.dig("data", "id")

OAuth 2.0 User Context (PKCE)

require "xdk"

client = Xdk::Client.new(
  client_id: ENV.fetch("X_CLIENT_ID"),
  client_secret: ENV["X_CLIENT_SECRET"],
  redirect_uri: "http://localhost:8080/callback",
  scope: %w[tweet.read users.read dm.read dm.write offline.access]
)

auth_url = client.get_authorization_url(state: "random-state")
# Redirect user to auth_url, then exchange the returned authorization code:
token = client.exchange_code(code)
me = client.users.get_me

Or pass an existing user access token directly:

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

OAuth 1.0a User Context

require "xdk"

oauth1 = Xdk::OAuth1.new(
  api_key: ENV.fetch("X_API_KEY"),
  api_secret: ENV.fetch("X_API_SECRET"),
  access_token: ENV.fetch("X_ACCESS_TOKEN"),
  access_token_secret: ENV.fetch("X_ACCESS_TOKEN_SECRET")
)
client = Xdk::Client.new(auth: oauth1)

Pagination

Paginated endpoints return a lazy Ruby Enumerator (or accept a block directly):

client.posts.search_recent(query: "ruby lang:en", max_results: 10).take(3).each do |page|
  (page["data"] || []).each { |post| puts post["text"] }
end

Streaming

config = Xdk::StreamConfig.new(
  max_retries: 10,
  idle_timeout: 60.0,
  on_keep_alive: -> { puts "heartbeat" },
  on_reconnect: ->(attempt, delay) { warn "reconnecting ##{attempt} in #{delay}s" }
)

client.stream.posts(stream_config: config) do |event|
  puts event.dig("data", "text")
end

Pairing with chat_xdk for Encrypted X Chat DMs

require "xdk"
require "chat_xdk"

api = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))
pub_keys = api.users.get_public_keys(id: recipient_id)["data"]

Development & Tests

bundle install
bundle exec rake test

License

MIT

About

Official Ruby SDK for the X API v2

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages