Skip to content

APIs

The API is defined in the api.js file, and all routes can be found in the /routes/ directory.

For example if you want to find the endpoints for the Chat functionality then can be found in /routes/chat.js.

API Definition

The API definition is an OpenAPI 3.0 spec, and is published as a website you can access from your dev server here:

http://localhost:1337/api

Koa

The API uses a HTTP router called Koa.

Koa is considered a successor to Express, and was designed by the same team.

If you’ve used Express in the past then Koa will be familiar. The main difference is that you return data from koa by setting ctx.body like this:

// POST /harmful/categories - fetches the categories
moderationRouter.post('/harmful/categories', async (ctx) => {
ctx.body = getCategories();
});

Calling the API

The API is secured by License Keys. When a user is created or signs up for your app, they are issued a license key automatically.

This license key can be used to make requests to the API as a Bearer Token in the Authorization header.

Authorization: Bearer sk_c8fcb59d-fad2-467c-b25e-dc4739d82785

Here’s an example of making an API request with the license key as a Bearer token from our demos:

const licenseKey = localStorage.getItem('licenseKey');
const response = await fetch('/api/images/create', {
method: 'POST',
headers: {
'Authorization': `Bearer ${licenseKey}`
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'Make an image of the Starship Enterprise orbiting an alien planet'
})
});