Skip to content

Secure an API ​

By default, an API's endpoints are public: anyone can call them. When security is enabled, the users of your API sign in to get a token (a JWT), then send it with every call. Each endpoint states which roles can call it.

This page is about REST API projects.

Enable security ​

Security is enabled when you create the project, with the Security Configuration option (see Create a project). It can't be enabled afterwards from the project edit screen.

On creation, Grizzly adds the Authentication Grizzly group, which holds the sign-in and user management endpoints.

The project's Security section ​

On the project page, below the endpoints, the Security section holds the settings. Click the arrow to open or close it.

Token Configuration ​

FieldPurpose
Client IDThe API's identifier. By default, the project name.
Secret Key (64 characters)The key that signs the tokens, generated by Grizzly. If you replace it, enter at least 64 characters, letters and digits only.
Token Expiration (in seconds)How long a token stays valid. Default: 3600 (one hour).

To change how long tokens stay valid, edit Token Expiration (in seconds), then click Save.

Manage Roles ​

A secured project starts with two roles: admin and user. To add one:

  1. Open the Manage Roles tab.
  2. Type the role name in the Add Role field, then click the Add Role button.
  3. Click Save.

The trash icon removes a role; remember to click Save afterwards. The admin role is locked (padlock icon) and can't be removed. If the role already exists, the message "This role already exists." is shown.

Choose who can call each endpoint ​

Each endpoint shows its access roles. Click them to open Access Roles and select public, all, admin, user or one of your custom roles. The details are in Resource groups and endpoints.

The roles of the endpoints in the Authentication Grizzly group are set by Grizzly and can't be changed.

Get a token ​

The examples in this section use the project's Runtime URL. The Security Documentation block, in the Security section, gives the same examples with your API's address already filled in, ready to copy.

Sign up a user ​

POST /signup creates an account. The firstname, lastname, username, password, email and phone fields are required, and email must be a valid address. Otherwise, the API responds 400 with a message naming the field at fault.

bash
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"firstname":"Mary","lastname":"Smith","username":"mary","password":"myPassword","email":"mary@example.com","phone":"0600000000"}' \
  https://<runtime-url>/signup

The new user gets the user role, but the account is inactive: an administrator must activate it (see below). If the username is already taken, the sign-up is refused.

Sign in ​

POST /signin checks the username and password, and returns a token:

bash
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"mary","password":"myPassword"}' \
  https://<runtime-url>/signin
json
{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1Ni..."
}

If the credentials are wrong, the API responds 401. If the account hasn't been activated yet, it returns the message Your account will be activated soon by the admin instead of a token.

Call a protected endpoint ​

Add the token to the Authorization header, preceded by Bearer:

bash
curl --header "Authorization: Bearer <token>" \
  https://<runtime-url>/products

When the token expires, sign in again with POST /signin.

Manage your API's users ​

The Authentication Grizzly group also contains endpoints to manage accounts. Call them with the token of a user who has the required role.

EndpointRequired roleWhat it's for
GET /meallThe signed-in user's profile
GET /allusersadminThe list of users
GET /allrolesadminThe list of roles
POST /activate?username=maryadminActivate an account created with /signup
PUT /grant?username=maryadminGive roles, with the body {"roles": ["user", "seller"]}
DELETE /deleteuser?username=maryadminDelete a user

With /grant, each role must exist in the Manage Roles tab, otherwise the API responds This role does not exist. Please enter a valid role.

What's next? ​

Grizzly API · CodeOnce Software