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:
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 categoriesmoderationRouter.post('/harmful/categories', async (ctx) => { ctx.body = getCategories();});
// POST /harmful/categories - fetches the categoriesmoderationRouter.post('/harmful/categories', async (req, res) => { return res.send(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' })});
import axios from 'axios';
const licenseKey = localStorage.getItem('licenseKey');
const response = await axios.post( '/api/images/create', { prompt: 'Make an image of the Starship Enterprise orbiting an alien planet' }, { headers: { 'Authorization': `Bearer ${licenseKey}`, 'Content-Type': 'application/json' } });
curl --request POST \ --url "http://localhost:1337/api/images/create" \ --header "Authorization: Bearer sk_test_fe638159-7d7a-4f75-b80d-eb5d270887a2" \ --data '{ "prompt": "Make an image of the Starship Enterprise orbiting an alien planet" }'