API Endpoints

Jumpstart Pro can provide API access to your application with API tokens and authentication already handled for you.

API Tokens

Every user in Jumpstart Pro can have multiple API tokens. They can create and revoke tokens on API section of their user settings.

Tokens keep track of the time they were last used so users can verify their integrations are working correctly and keep an eye on them.

API Authentication

Authentication with the Jumpstart Pro API is as simple as passing in the Authorization header. We provide an example of this header on the API token page to make it easy for users to implement

An example Authorization header would look like the following:

GET /api/v1/me.json
Authorization: Bearer exampletoken

If you'd like to authenticate users through the API using their email and password to retrieve an API token, you can make a POST request to the auth endpoint with the user's email and password. This is handy for authenticating from a mobile app, etc.

POST /api/v1/auth.json
Params: { email: "steve@apple.com", password: "hunter2" }

This will return a JSON response with a token you can use to access the API.

{ token: "abcd1234" }

API Controllers & Routes

API controllers are defined in app/controllers/api/v1/. We provide a /api/v1/me.json and /api/v1/accounts.json endpoint out of the box. You can use these controllers as examples to base your API endpoints off of.

API controllers should inherit from Api::BaseController instead of ApplicationController. This enables API authentication. To skip authentication on an endpoint, add skip_before_action :require_api_authentication to your actions.

Authentication works with the app's session and API tokens so you can access current_user just like normal.

Since APIs don't have sessions, there is no current_account equivalent like in the main app. You can use nested resources in the API to scope things under accounts if you want to separate them. For example: /api/v1/accounts/2/projects/4.json.

API Token Scopes

Scopes are off by default, so every token can do anything its user can. Configure them in config/initializers/api_tokens.rb to let users, and OAuth applications, limit a token to part of your API. Each controller declares the scope its actions need:

class Api::V1::ProjectsController < Api::BaseController
  require_api_scope "projects:read", only: [:index, :show]
  require_api_scope "projects:write", except: [:index, :show]
end

A token without the scope gets a 403 with WWW-Authenticate: Bearer error="insufficient_scope". require_api_scope does nothing until scopes are configured, and requests signed in with a cookie, like Hotwire Native, aren't limited. Tokens created by signing in through /api/v1/auth get every scope. For finer checks inside an action, use api_token.can?("projects:publish").

The built-in endpoints need accounts:read, users:read, users:write and notification_tokens:write, so keep those resources in your config when you enable scopes.

API Responses

API controllers are configured to return JSON by default.

We use JBuilder by default to provide simple JSON templates that most users are already familiar with.

We also recommend checking out the fast_jsonapi gem from Netflix if you'd like to use an alternative.