Skip to content
Gravity Logo / Docs Gravity home
Browse documentation
Documentation/Gravity Server

REST API

The API files are located in the /api folder in the Node.js version and /src/api in the Next.js version.

The structure of these files is simple; there is a list of endpoints that connect directly to the relevant controller method.

// node.js
api.post('/api/account', use(accountController.create));

// next.js
export const POST = withApiRoute(accountController.create);

Each controller call is wrapped in a higher-order function. This is a middleware function that catches any errors in the controller methods and then passes these to a global error handler – this prevents you from having to use try...catch in your application.

Protected Routes

You can protect any API route and make it accessible to only a specific user level or API key scope using the auth.verify middleware method. You simply pass the user permission as the first argument, and an optional API scope as the second.

// protect using a user permission
api.get('/api/user', auth.verify('user'), use(userController.get));

// protect using a user permission and api scope
api.get('/api/user', auth.verify('user', 'user.read'), use(userController.get));

// next.js
export const GET = withApiRoute('owner', 'user.read', userController.get);

Learn more about API scopes and find out more about how authentication works in the next section.

Accessing The API

You can access the API using one of two methods:

  1. Using a Bearer token issued during the authentication flow
  2. Using an API key

Using Bearer Tokens

Bearer tokens are JWTs that are used to authenticate the user. You can use a Bearer token with the API by passing it in the Authorization header.

Authorization: Bearer your_jwt

Using API Keys

API keys can be created by either an owner or developer permission and can be used to access any endpoint that has an API scope (see Protected Routes above).

The SQL key model stores the key value, masks it in list responses and returns it when retrieving an individual key. Treat full keys as credentials.

Send the key in the Authorization header. For example, a key with the account.read scope can read its account:

const res = await axios.get('https://yourdomain.com/api/account', {
  headers: { Authorization: 'Basic ' + apiKey }
});

Input Validation

Input validation is handled with JOI, which has been wrapped in a HOF to support locales.

// validate
const data = utility.validate(joi.object({
    
    email: joi.string().email().required(),
    name: joi.string().required().min(3).max(100),
    password: joi.string().required().pattern(new RegExp(config.get('security.password_rules'))),
    confirm_password: joi.string().allow(null),
    verify_view_url: joi.string().allow(null)

}), req, res);

Rate Limiting

API requests are globally rate-limited as defined by the throttle settings in config. The following end points have their own lower rate limits for security purposes:

  • POST /api/account
  • POST /api/user
  • POST /api/auth
  • POST /api/auth/password/reset/request
  • POST /api/auth/password/reset

API Logs

The Node.js authentication middleware records successful scoped API-key requests when ENABLE_API_LOGS is set to the string true. Application errors are recorded separately by the server error handler.

ENABLE_API_LOGS=true

Swagger File

There is a swagger file included in api/spec.yaml with documentation and examples for each endpoint using the OpenAPI 3.0 spec. You can import this into Postman for easy testing.