Rydmate API

Rydmate Ruby API

Connect your Ruby application to Rydmate: read the fleet, hold seats and hear about every change. There is no separate library to install. The API speaks plain HTTPS and JSON, so Ruby's own tools are enough.

Ruby 2.6 or laterAPI reference

Before you start

  • Create an API key in Rydmate under Developers. A key starting with ryd_test_ works on your test account, so nothing you try touches real trips or money.
  • Set it as RYDMATE_API_KEY in your environment. Never put the key in your code or your app.
  • No gems to install: net/http, json and openssl come with Ruby.
1

List your vehicles

The simplest request: every vehicle in the business, with its registration and status. If something goes wrong, the error says what, and carries a reference our team can look up.

list.rb
require "json"
require "net/http"

api = ENV.fetch("RYDMATE_API_URL", "https://api.rydmate.com/api/v1")
uri = URI("#{api}/vehicles")

request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer #{ENV.fetch('RYDMATE_API_KEY')}"

response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) }
body = JSON.parse(response.body)
unless response.is_a?(Net::HTTPSuccess)
  error = body["error"]
  raise "#{error['code']}: #{error['message']} (#{error['request_id']})"
end

body["data"].each { |vehicle| puts "#{vehicle['registration_number']} #{vehicle['status']}" }

Run it with ruby list.rb.

2

Hold a seat without double bookings

Hold a seat on a departure for a passenger. The Idempotency-Key header means that if the connection drops and you send the same request again, you still get one hold, not two.

If another counter took the seat first, Rydmate answers with a conflict instead of booking it twice. Once the passenger has paid, confirm the hold with POST /bookings/{id}/transition and {"to": "CONFIRMED"}.

book.rb
require "json"
require "net/http"
require "securerandom"

api = ENV.fetch("RYDMATE_API_URL", "https://api.rydmate.com/api/v1")
uri = URI("#{api}/trips/#{ENV.fetch('TRIP_ID')}/bookings")

request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{ENV.fetch('RYDMATE_API_KEY')}"
request["Content-Type"] = "application/json"
# Keep this key and send it again on a retry: you get one hold, never two.
request["Idempotency-Key"] = SecureRandom.uuid
request.body = {
  seat_number: "3A",
  passenger: { full_name: "Ngozi Okafor" },
  channel: "PUBLIC_WEB"
}.to_json

response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) }
body = JSON.parse(response.body)

case response.code.to_i
when 409 then puts "Seat already taken: #{body['error']['message']}"
when 200..299 then puts "Seat held #{body['data']['id']}"
else raise "#{body['error']['code']}: #{body['error']['message']}"
end
3

Check that an update really came from Rydmate

Rydmate tells your system the moment a trip, ticket, delivery or payment changes. Each update is signed with your endpoint's secret. Check the signature before you act on it, and refuse anything older than five minutes.

verify.rb
require "openssl"

# Checks the Rydmate-Signature header on a webhook delivery.
# raw_body must be the request body exactly as it arrived.
def verify_rydmate_signature(secret, header, raw_body, tolerance: 300)
  parts = header.split(",").map { |part| part.strip.split("=", 2) }.select { |pair| pair.size == 2 }.to_h
  t = parts["t"].to_s
  v1 = parts["v1"].to_s
  return false unless t.match?(/\A\d+\z/) && !v1.empty?
  return false if (Time.now.to_i - t.to_i).abs > tolerance

  expected = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{t}.#{raw_body}")
  return false unless expected.bytesize == v1.bytesize

  expected.bytes.zip(v1.bytes).reduce(0) { |acc, (a, b)| acc | (a ^ b) }.zero?
end

Build on Rydmate

Connect your product to real transport operations

Start on a test account, then go live when your integration is ready. Book a demo to talk through what you are building.