Instagram and TikTok API in Ruby
A standard-library Ruby client: Net::HTTP, JSON and an Enumerator for cursor paging, so .first(100) or .take_while stops calling when you have enough.
Short answer. Net::HTTP::Get with the X-Api-Key header, JSON.parse the body and follow pagination.next_cursor. The module below wraps that with retries and a lazy paginate; drop it into a Rails app or a script.
Facts checked 2 December 2026. Prices change: follow the links before you decide.
Set up
- Ruby 3 or newer; no gems. Set
HANDLEHAUL_API_KEYin the environment or Rails credentials and pass it asapi_key:. - For Rails background jobs, build one client per job run; the client only keeps a credit counter.
The client module
get retries 429 after Retry-After and 502/503/504 with a growing wait (free answers), and raises HandleHaul::Error with status and code otherwise.
paginate yields rows page by page; without a block it returns an Enumerator, so .first(5) stops after the page that holds the fifth row. Each page is one call.
handlehaul.rbRun: ruby handlehaul.rb
# A small HandleHaul API client for Ruby 3 (standard library only): retries, cursor paging and clear errors.
# export HANDLEHAUL_API_KEY=kd_live_... # or a free kd_test_ key for sample data
# ruby handlehaul.rb
require "json"
require "net/http"
require "uri"
module HandleHaul
API = "https://api.handlehaul.com/v1"
RETRY = %w[429 502 503 504].freeze # never charged; other 4xx answers will not change on a retry
class Error < StandardError
attr_reader :status, :code
def initialize(status, code, message)
@status = status
@code = code
super("#{status} #{code}: #{message}")
end
end
class Client
attr_reader :credits # what this client spent, from meta.credits_charged
def initialize(api_key: ENV.fetch("HANDLEHAUL_API_KEY"), max_tries: 5)
@api_key = api_key
@max_tries = max_tries
@credits = 0
end
# One call. Retries 429 (after Retry-After) and 502/503/504 (growing wait).
def get(path, params = {})
uri = URI(API + path)
uri.query = URI.encode_www_form(params) unless params.empty?
@max_tries.times do |attempt|
req = Net::HTTP::Get.new(uri)
req["X-Api-Key"] = @api_key
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 60) { |h| h.request(req) }
if RETRY.include?(res.code) && attempt < @max_tries - 1
sleep((res["Retry-After"] || (2**attempt) + rand).to_f)
next
end
body = JSON.parse(res.body)
raise Error.new(res.code.to_i, body.dig("error", "code"), body.dig("error", "message")) unless res.is_a?(Net::HTTPSuccess)
@credits += body.dig("meta", "credits_charged").to_i
return body
end
raise Error.new(429, "rate_limited", "still limited after retries")
end
# Every row of a list endpoint, one page (one call) at a time. Stop the loop to stop paying.
def paginate(path, params = {}, max_pages: 10)
return enum_for(:paginate, path, params, max_pages: max_pages) unless block_given?
params = params.dup
max_pages.times do
body = get(path, params)
body["data"].each { |row| yield row }
break unless body["pagination"]["has_more"]
params["cursor"] = body["pagination"]["next_cursor"]
end
end
end
end
if __FILE__ == $PROGRAM_NAME
hh = HandleHaul::Client.new
tag = hh.get("/tiktok/hashtags/booktok")["data"]
puts "##{tag["name"]}: #{tag["post_count"]} posts, #{tag["view_count"]} views"
hh.paginate("/tiktok/hashtags/booktok/posts", max_pages: 2).first(5).each do |post|
puts [post["created_at"], post.dig("counts", "views"), post["url"]].join(" ")
end
begin
hh.get("/instagram/users/private_demo/followers")
rescue HandleHaul::Error => e
puts "expected error: #{e.code}"
end
puts "credits used: #{hh.credits}"
end
Every endpoint in Ruby
Each page below has a complete Ruby program for one route, the parameters, a sample response and what it costs. The link opens the Ruby tab.
| Tutorial | Route |
|---|---|
| Profile | GET /v1/instagram/users/{user} |
| Account details | GET /v1/instagram/users/{user}/account-details |
| Similar accounts | GET /v1/instagram/users/{user}/related |
| Followers | GET /v1/instagram/users/{user}/followers |
| Following | GET /v1/instagram/users/{user}/following |
| Posts | GET /v1/instagram/users/{user}/posts |
| Reels | GET /v1/instagram/users/{user}/reels |
| Tagged posts | GET /v1/instagram/users/{user}/tagged |
| Reposts | GET /v1/instagram/users/{user}/reposts |
| Post or reel | GET /v1/instagram/posts/{post} |
| Post stats (batch) | GET /v1/instagram/posts/stats |
| Comments | GET /v1/instagram/posts/{post}/comments |
| Comment replies | GET /v1/instagram/comments/{comment}/replies |
| Post likers | GET /v1/instagram/posts/{post}/likers |
| Comment likers | GET /v1/instagram/comments/{comment}/likers |
| Stories | GET /v1/instagram/users/{user}/stories |
| Story by id | GET /v1/instagram/stories/{story} |
| Highlights | GET /v1/instagram/users/{user}/highlights |
| Highlight stories | GET /v1/instagram/highlights/{highlight} |
| Search hashtags | GET /v1/instagram/search/hashtags |
| Hashtag | GET /v1/instagram/hashtags/{tag} |
| Hashtag posts | GET /v1/instagram/hashtags/{tag}/posts |
| Search places | GET /v1/instagram/search/places |
| Place | GET /v1/instagram/places/{place} |
| Place posts | GET /v1/instagram/places/{place}/posts |
| Search audio | GET /v1/instagram/search/audio |
| Trending audio | GET /v1/instagram/audio/trending |
| Audio | GET /v1/instagram/audio/{audio} |
| Reels using audio | GET /v1/instagram/audio/{audio}/reels |
| Search accounts | GET /v1/instagram/search/users |
| URL resolver | GET /v1/instagram/resolve |
TikTok
| Tutorial | Route |
|---|---|
| Profile | GET /v1/tiktok/users/{user} |
| Suggested accounts | GET /v1/tiktok/users/{user}/suggested |
| Followers | GET /v1/tiktok/users/{user}/followers |
| Following | GET /v1/tiktok/users/{user}/following |
| Posts | GET /v1/tiktok/users/{user}/posts |
| Reposts | GET /v1/tiktok/users/{user}/reposts |
| Liked videos | GET /v1/tiktok/users/{user}/liked |
| Playlists | GET /v1/tiktok/users/{user}/playlists |
| Stories | GET /v1/tiktok/users/{user}/stories |
| Video or photo post | GET /v1/tiktok/posts/{post} |
| Comments | GET /v1/tiktok/posts/{post}/comments |
| Comment replies | GET /v1/tiktok/comments/{comment}/replies |
| Transcript | GET /v1/tiktok/posts/{post}/transcript |
| Sound | GET /v1/tiktok/music/{music} |
| Posts using a sound | GET /v1/tiktok/music/{music}/posts |
| Hashtag | GET /v1/tiktok/hashtags/{tag} |
| Hashtag posts | GET /v1/tiktok/hashtags/{tag}/posts |
| Search | GET /v1/tiktok/search |
Ruby: questions
Is there a Ruby gem for HandleHaul?
Faraday or Net::HTTP?
Retry-After, and never retry 400, 401, 402, 403 or 404.How do I export rows to CSV in Ruby?
require "csv", open a file, and write csv << row.values_at("username", "full_name") inside the paginate block. Rows reach the file as pages arrive, so an interrupted run keeps what it fetched.Related
Instagram and TikTok API in Python
Two ways to call HandleHaul from Python: a 60-line client on requests that you own, or the official handlehaul package (sync and async).
Read more →Instagram and TikTok API in Node.js
Call HandleHaul from Node.js 18 or newer with the built-in fetch, or install the typed handlehaul package from npm, which also ships a CLI.
Read more →Instagram and TikTok API in PHP
One class, PHP 8 and the curl extension: no Composer package needed.
Read more →Build it in Ruby
One key for Instagram and TikTok. $1.40 to $0.80 per 1,000 calls; failed calls and cache hits are free.
Want to test first? Sandbox keys are free and unlimited, with sample data in the live schema.