Skip to content

API Quick Start Guide

Introduction

Welcome developer, to the Charopia API! This is brief overview of how to get started using the API to leverage the content and functionality of Chartopia.

For ease of reading, this guide will use a placeholder API-Key of ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK to avoid having to use asterisks.

Basic overview

The Chartopia API is a REST API allows access to data on Chartopia. This includes charts, collections, folders, roll history etc. There are also functional endpoints such as rolling on charts to generate a result, or generating results using the Chartopia Domain Language as a parameter.

To have access to the API you will require two things:

  1. An account with d12dev.com (Chartopia)
  2. To have created your own Project that has an assigned API Key.

Note that a Project's API key is not for authentication (i.e. identifying the user), but rather authorization, i.e. determining the level of access that is granted to that user. This means that if you create an app, e.g. a mobile app, any Chartopia user can log in using the Chartopia API, but the required API Key will restrict what the user can and cannot do.

There is currently only a free tier, and this tier is a restricted in two ways:

  1. Throttling/Rate limiting: There are restriction to how many requests can be made per minute and per day.
  2. Daily quota: There is a limit to the number and type of requests (and responses) made to and retrieved from the API.

These restrictions are subject to change at any time.

Data/content visibility is enforced; private data is only accessible to the user that owns that data, once they've logged in to Chartopia via the API.

Daily Quota Restrictions

Each Project identified with an API Key is allocated a certain amount of daily quota "points". Every call to the API has a "cost", and for endpoints that return a lot of data, there is also a "cost" associated with the amount of data returned.

As a rule of thumb, each call the API costs 1 point, and each instance of returned data costs 0.1 point. For example, and call to /api/charts/ will cost 1 point, and each search result returned will cost an additional 0.1 points.

All response headers contain information about the quota status.

  • X-API-Quota-Request: The number of points used by the request.
  • X-API-Quota-Used: The number of points used in total today. This number resets to zero at midnight UTC.
  • X-API-Quota-Left: The number of points left for the day.

If the quota is fully consumed for the day, the response will have a 402 status code.

How to create a Project

Once you've created an account, go to the Projects page and click "Create Project".

Enter and name and description and press the "Rest API Key" button to generate an API Key. Be sure to copy this key for use with all your API requests, and never share it.

Create project

NB: You can edit the Project meta data at any time, or reset the API Key if you forget it. Note this this will revoke the existing API-Key for this project and will prevent any apps using it from accessing the API.

Examples

Simple, anonymous chart rolling

The most simple API endpoint is the humble roll/generate on a public chart. This is one of the few endpoints where a user does not have to be logged in, so it makes for a good example to explore the tools. Ensure you have your API-Key handy because it will be required.

Using the Swagger Playground

First, you need to "Authorize" using your API-Key. The button is at the top of the Swagger screen and will open the following dialog for you to add a key.

Authorize

This will make all subsequent requests populate the X-API-Key header field with your API key.

Scroll down to the POST endpoint called /charts/{public_id}/roll/

Here, you can set the public_id of the chart, some parameters (in this case, a basic roll), the press execute to see the result.

A public_id is an 11 character string that looks something like nikp0hG2dN8. It is not the same as the numeric ID that is seen in urls, or for linking to other tables. You may recognise this character format as being similar to the IDs using in YouTube urls, e.g. https://www.youtube.com/watch?v=dQw4w9WgXcQ

Eventually all Chartopia's urls will transition to using this public id format, but for now, it is used only for the API.
The public_id of a Chart is visible either in the top left of the Chart editor toolbar, or at the bottom right of the Chart pages. You must have created a Project with a valid API-Key, and be signed in, for these to be visible.

Chart roll

Chart roll result

Postman

In Postman

  • Open a new tab
  • Set to be a POST request
  • Set the url to be https://chartopia.d12dev.com/api/charts/nikp0hG2dN8/roll/
  • Set the header to have a key of X-Api-Key and a value of your API Key.

Chart roll

Notice that in the return headers, you can see the X-Api-Quota-Request, X-Api-Quota-Used and X-Api-Quota-Left with information about the daily quota against your Project.

Response headers

cURL request

For a simple chart roll, you can use the the following cURL request.

curl -X POST https://chartopia.d12dev.com/api/charts/nikp0hG2dN8/roll/ -H "X-Api-Key: ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK"

Python request

A simple chart roll can be achieved in Python using the requests package.

import requests

url = "https://chartopia.d12dev.com/api/charts/nikp0hG2dN8/roll/"
headers = {
    "X-Api-Key": "ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK"
}
response = requests.post(url, headers=headers)

print(response.status_code)
print(response.json())

User authenticated Chart roll example (complex)

The simple chart rolling examples work only if the chart has been made public by the owner of the chart. That is because the API call is done with an Anonymous user. A few other API endpoints, such as the /gen/ can also be done anonymously.

To allow users to roll on their own private content, it's necessary to authenticate first.

It's important to note that distinction: authentication is to identify the users (via username password), but the API Key is only to identify a Project that is using the API. API Keys are not considered secure because a client application (i.e. your app), could easily expose it and allow it to be shared. Please don't allow your API Key to be exposed, because any abuse will result in the API Key being revoked and your application will no longer work. There's more information about the distinction here.

Authentication is process of requesting and sending tokens from the Chartopia server. Upon a log in request, two tokens are returned, one is long lived (called the refresh token) and one is short lived (called the access token). The access token is required to sent for all requests to the Chartopia API. Because it is short lived, it will eventually expire and will require you, as the developer, to request a new short lived token by using the long lived token.

Remember, the user logging in isn't necessarily the user (you) who made the Project (that uses the API Key). This is so any user can be using your own apps using Chartopia's API.

The process looks a bit like the following.

Using the Swagger Playground

  • Go to the top right of the page and select Authorize.
  • Enter the Project's API Key into the ApiKeyAuth (apiKey) section.
  • Go to the auth/login endpoint
  • Select "Try it out"
  • Enter the username and password for a valid user (e.g. yourself) into the json structure. You can use username or email address.
  • Press Execute

Login

The response will include an access token and refresh token. Use this for the subsequent steps

Access token and refresh token

Your application will need to use the access token to make requests to the Chartopia API. Just like for the API key, this needs to be added to the Authorization popup. Scroll to the bottom and add the access token value to tokenAuth (apiKey) and press Authorize. You must prefix your access key with "Bearer ", e.g. Bearer eyJhbGciOiJIUzI1NiIsInR5...
Ignore the naming, it's a bit of misnomer.

Access token authentication

The access token is short lived, and will only work for a few minutes at which point a 401 status code will be returned. This is a security feature of token based authentication. Rather than having to login again, it's a simple matter of generating a new access token by making a request to /api/auth/token/refresh/ with the refresh token as data.

Token refresh

The response will provide a new, short lived, access token. If the refresh token expired, then the user will need to login again.

With both the API Key and access token added to the Swagger playground, it should be possible for users to make requests for their private data.

Postman

The Postman process is similar to Swagger's playground. To login, send a post request to /api/auth/login/ (don't forget the trailing forward slash), and send raw json data.

Postman login

The access token can be used in conjunction with the API Key to allow a user to make requests for their private data. This is set in the Headers section with X-Api-Key set to you API Key and Authorization set to Bearer eyJhbGciOiJIUzI1NiIsInR5... i.e. "Bearer is the prefix to the full access token value.

Postman roll with authorization

cURL

Note that cURL commands are generated from the Swagger playground. Regardless, here are some simple examples.

A cURL commmand for logging in and receiving the access and refresh token is the following.

curl -X POST \
  'https://chartopia.d12dev.com/api/auth/login/' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "YourSaturdayNightGM",
  "password": "Th1s!sT0tallyS3cret"
}'

To make a roll on a private chart of the logged in user.

curl -X 'POST' \
  'https://chartopia.d12dev.com/api/charts/khd5vn80KHs/roll/' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQ1ODM3NDQ3LCJpYXQiOjE3NDU4MzcxNDcsImp0aSI6ImFhYjkxNmQ1NDgxYzQyZjQ5ZTBmOTkyNWRkMTFmOTQ4IiwidXNlcl9pZCI6MTgxNjF9.fkSmAnhG3yetdrLgpSX-lCvyK5Hx8H6q6O10VEbNbXc' \
  -H 'X-Api-Key: ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK' \
  -H 'Content-Type: application/json' \
  -d '{}'

or more succinctly

curl -X 'POST' \
  'https://chartopia.d12dev.com/api/charts/khd5vn80KHs/roll/' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQ1ODM3ODkyLCJpYXQiOjE3NDU4MzcxNDcsImp0aSI6ImZiNDM4NTJlNzE3ZTQ1YTQ5NjBiNTA1ZDQxNDBiNGMwIiwidXNlcl9pZCI6MTgxNjF9.JEGBMEXY6HWM5J05ixtzK7gsOSh4K41Vj7RhlkVJnzo' \
  -H 'X-Api-Key: ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK'

To refresh the access token with the refresh token, go

curl -X POST \
  'https://chartopia.d12dev.com/api/auth/token/refresh/' \
  -H 'Content-Type: application/json' \
  -d '{"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc0NjQ0MTk0NywiaWF0IjoxNzQ1ODM3MTQ3LCJqdGkiOiJlZjMwODJkMjU4MDc0ZGNjYWFlMzE4ZDljZWQ3ZDNmNSIsInVzZXJfaWQiOjE4MTYxfQ.IW2x8Wa7DEnVOjE-g6lJmIqrMm1_qmrYn-LQibGjixg"}'

Python

For logging in

import requests

url = "https://chartopia.d12dev.com/api/auth/login/"
headers = {
    "Content-Type": "application/json"
}
data = {
    "username": "YourSaturdayNightGM",
    "password": "Th1s!sT0tallyS3cret"
}

response = requests.post(url, headers=headers, json=data)

For rolling

import requests

url = "https://chartopia.d12dev.com/api/charts/khd5vn80KHs/roll/"
headers = {
    "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQ1ODM3ODkyLCJpYXQiOjE3NDU4MzcxNDcsImp0aSI6ImZiNDM4NTJlNzE3ZTQ1YTQ5NjBiNTA1ZDQxNDBiNGMwIiwidXNlcl9pZCI6MTgxNjF9.JEGBMEXY6HWM5J05ixtzK7gsOSh4K41Vj7RhlkVJnzo",
    "X-Api-Key": "ZJZeAqOg.vaw3XPKwYthW5Myo5BhI4yBrpVv76AuK"
}

response = requests.post(url, headers=headers)

For refreshing the access token.

import requests

url = "https://chartopia.d12dev.com/api/auth/token/refresh/"
headers = {
    "Content-Type": "application/json"
}
data = {
    "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTc0NjQ0MTk0NywiaWF0IjoxNzQ1ODM3MTQ3LCJqdGkiOiJlZjMwODJkMjU4MDc0ZGNjYWFlMzE4ZDljZWQ3ZDNmNSIsInVzZXJfaWQiOjE4MTYxfQ.IW2x8Wa7DEnVOjE-g6lJmIqrMm1_qmrYn-LQibGjixg"
}

response = requests.post(url, headers=headers, json=data)

Executing Chartopia Domain Language Code

It is possible to execute raw Chartopia Domain Language code via the /gen/ endpoint. For example, the roll_chart function has a multitude of arguments that aren't available to the /charts/{public_id}/roll/ endpoint, so we encourage the use of /gen/ to take advantage of its flexibility.

A typical use case is to specify a limited dice range on an existing table. The Star Wars loot table is a d100, so to restrict the roll to the first 10 rows, one would execute {{roll_chart id:288 dice:"d10"}}. As a curl request, it would look like the following.

curl -X 'POST' \
  'https://chartopia.d12dev.com/api/gen/' \
  -H 'accept: application/json' \
  -H 'X-Api-Key: FEM39pBJ.9ua2yHAkWFkVzyslNsVXFLhR26Y69Dvj' \
  -H 'Content-Type: application/json' \
  -d '{"raw": "{{roll_chart id:288 dice:\"d10\"}}"}'

NB: The Authorization header isn't required so long as the content rolled on is public.

Checkout our github examples

We do have a single HTML and Javascript example on the github page.

We intend to add more complex examples to provide starting points and ideas for developers.

Getting Support

The best place to get assistance is on the Chartopia subreddit.

Remember, never share you API Key with anyone. Ensure that your API Key is not visible to users of your application.