Jumpstart docs logo Back to the app

API Clients

API Clients

Jumpstart Pro includes a built-in API client generator. You can use it to create your own API clients for integrations with any API you would like.

As developers, it's easy to reach for Rubygems, but each dependency we add makes it harder to maintain your application. We've found that we need to fork most API Rubygems to fix bugs or implement API features that aren't supported yet in the library.

Now, our default is to build an API client ourselves. It's easy to maintain, doesn't break from external changes, and only implements the features we need. We've made this available to Jumpstart Pro users to more easily take advantage of building their own API clients.

To learn more about API Clients in Jumpstart Pro, check out the Net Http API Client from Scratch video on GoRails.

Creating API Clients

Run the API client Rails generator to create an API client.

rails g api_client OpenAi

This generates app/clients/open_ai_client.rb and a matching test file.

To implement API endpoints, you will define methods that make an HTTP request to the API endpoints. Here's an example using the Sendfox API:

class SendfoxClient < ApplicationClient
  BASE_URI = "https://api.sendfox.com/"

  def me
    get "/me"
  end

  def lists
    get "/lists"
  end

  def list(id)
    get "/lists/#{id}"
  end

  def create_list(name:)
    post "/lists", body: {name: name}
  end

  def remove_contact(list_id:, contact_id:)
    delete "/lists/#{list_id}/contacts/#{contact_id}"
  end

  def contacts(email: nil)
    if email
      get "/contacts?email=#{CGI.escape(email)}"
    else
      get "/contacts"
    end
  end

  def contact(id)
    get "/contacts/#{id}"
  end

  def create_contact(**params)
    post "/contacts", body: params
  end

  def unsubscribe(email:)
    patch "/unsubscribe", body: {email: email}
  end

  def campaigns
    get "/campaigns"
  end

  def campaign(id)
    get "/#{id}"
  end
end

ApplicationClient

ApplicationClient is the base class that API clients inherit from. It provides methods for get, post, patch, put, and delete. It also handles authorization using the Authorization header and parsing response bodies as JSON.

You can override these methods in your API client to match your API. For example, if you need to parse XML instead of JSON, override the parse_response method and use Nokogiri instead.

More features

Optionally pass the --url option to set the base URL

Optionally pass methods with index, show, create, update, destroy types

rails g api_client OpenAi completions:create --url https://api.openai.com