Using the Mojo API

Using the Mojo API

The Mojo API allows users to interact programmatically with their Mojo instance. Below is a guide to help you get started.

Exploring the Mojo API

To begin using the Mojo API, log in to your Mojo instance and select any of the three options on the dashboard:

  1. Schema - Raw access to the API schema in YAML or JSON.
  2. Swagger - Good for API testing and ease of use.
  3. ReDoc - Best for coders to understand how the API functions with examples.

An OpenAPI Specification is also available outside of a Mojo installation for reference and to build external tools against. You can browse and access the specifications.

API Endpoint

The API endpoint is derived from your Mojo instance’s URL by adding /api/ at the end. For example:

  • If your Mojo instance is located at https://7.7.7.7, then:
    • Web UI URL: https://7.7.7.7
    • API Endpoint: https://7.7.7.7/api/

In documentation, ${MOJOHOST}/api/ is used as a placeholder for your specific API endpoint.

Authentication: Obtaining a JWT Token

To interact with the API, you need to authenticate by obtaining a JWT token. Follow these steps:

Step 1: Send a POST Request

Send a POST request with your username and password to the /api/token/ endpoint.

Using HTTPie:


TOKEN=$(http --verify no POST ${MOJOHOST}/api/token/ username=${MOJOUSER} password=${MOJOPASS} | jq -r '.access')

Using cURL:


TOKEN=$(curl -s -L -k -H "Content-Type: application/json" -d '{"username": "'"${MOJOUSER}"'", "password": "'"${MOJOPASS}"'"}' ${MOJOHOST}/api/token/ | jq -r .access)

Replace:

  • ${MOJOHOST} with your API endpoint.

  • ${MOJOUSER} and ${MOJOPASS} with your username and password.

Step 2: Token Validity

The token you receive is valid for 24 hours. Use it in the Authorization header for subsequent requests.


Testing Your Token

To verify that your token works, send a GET request to /api/users/me/.

Using HTTPie:


http --verify no ${MOJOHOST}/api/users/me/ Authorization:"Bearer ${TOKEN}"

Using cURL:


curl -s -L -k -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" "${MOJOHOST}/api/users/me/"

This request will return details about the authenticated user.


OpenAPI Specification

The OpenAPI Specification provides detailed documentation of all endpoints, parameters, and responses. It can be used to design, validate, and integrate external tools with the Mojo API.