Building a game with the API
This guide describes how a program – for example an MCP server used by an AI agent – builds a game with the API. Read Stories, challenges and elements first. The complete working example is the tutorial The Clockmaker’s Key.
All paths are relative to https://api.toto.io, {repoId} is the repository, E stands for /v2/edit/{repoId}.
0. Check access
Section titled “0. Check access”GET /v2/apikey/self (with the api key) lists the repositories the key can use and the role in each. Building content
requires the role Author and an Active repository.
1. Create, then update
Section titled “1. Create, then update”Every entity is created with a small POST and then completed with a PUT that contains all fields:
| Entity | Create | Update |
|---|---|---|
| area | POST E/area { "name" } |
PUT E/area/{id} { name, content, locations } |
| variable | POST E/var { "name", "scope", "t", "defaultValue" } |
PUT E/var/{id} { content, … } |
| element | POST E/element { "name", "t" } |
PUT E/element/{id} – full element |
| challenge | POST E/challenge { "name" } |
PUT E/challenge/{id} – full challenge incl. graph |
| story | POST E/story { "name" } |
PUT E/story/{id} – full story incl. graph |
| file | PUT E/file (multipart, field file) |
– |
| voucher | POST E/voucher { "storyId", "name", "maxCount", "sessionVarValues": [] } |
PUT E/voucher/{id} |
The update of an element replaces the element: always send name, content, solutions, score, trigger and
metaTags, even if they are empty.
PUT E/element/{id}{ "name": "time-zones", "content": { "title": [{ "lang": "eng", "text": "Count the time zones" }], "description": [{ "lang": "eng", "text": "How many time zones does the World Clock show?" }], "publicInfo": [], "postDescription": [], "choiceDescription": [], "files": [], "successMessage": [{ "lang": "eng", "text": "Correct!" }], "errorMessage": [], "showSuccessDialog": true, "showErrorDialog": true, "showOnList": true }, "solutions": { "mc": { "choices": [ { "value": "12", "isCorrect": false, "score": 0 }, { "value": "24", "isCorrect": true, "score": 10 }, { "value": "36", "isCorrect": false, "score": 0 } ] } }, "score": 10, "trigger": [{ "t": "OnElementSuccess", "order": 1, "script": { "parentId": "<id of keysFound>", "expr": "{{d:keysFound}} + 1", "lang": "Javascript" } }], "metaTags": []}2. Order of building
Section titled “2. Order of building”Ids are needed before they can be referenced, so build bottom up:
- Files – upload images, keep the returned ids (logos, embedded images).
- Areas – needed by geofence solutions.
- Variables – needed by trigger scripts (
parentId) and texts. - Elements – create all, then update them. Elements that reference other elements (dynamic scripts) are updated after their targets exist.
- Challenges – graph of element ids.
- Story – graph of challenge ids, coins in
associatedElements,supportedLanguages. - Voucher – to play the story.
3. Graphs
Section titled “3. Graphs”Graph nodes have their own ids; links and start / finishedOn use the node ids, refId the element or
challenge id:
{ "start": ["a"], "finishedOn": ["c"], "nodes": [ { "id": "a", "refId": "<welcome>", "posX": 0, "posY": 0 }, { "id": "b", "refId": "<reach>", "posX": 0, "posY": 120 }, { "id": "c", "refId": "<time-zones>", "posX": 0, "posY": 240 } ], "links": [ { "source": "a", "target": "b" }, { "source": "b", "target": "c" } ]}4. Check the game
Section titled “4. Check the game”- Every path of every challenge graph reaches a
finishedOnnode. - Every choice and dynamic element has at least two successors; a dynamic script returns the id of one of them.
- Every text has a translation for every language in
supportedLanguages. - Geofence areas are large enough (30–50 m).
- No game logic depends on
OnStoryFinish,OnStoryCancel,OnStoryJoinorOnObjectMessageReceived– these triggers are not executed, see Triggers.
5. Test by playing
Section titled “5. Test by playing”API keys can not play. Play the story with a player account (session cookie) through the session endpoints: init with the voucher token, start, process the elements with their session element ids, end the session, then check the session status and the variables.
Pitfalls
Section titled “Pitfalls”Error codes are explained in Error codes, valid values of fields like t or scope in
Enumerations.
| Problem | Cause |
|---|---|
403 on create/update |
repository not Active, key is readOnly, or endpoint not available with api keys |
400 edit:52 |
the account has no or not enough role in the repository |
| elements are not released | link points to an element id instead of a node id, or start is empty |
| a story never ends | the session has to be ended with POST /v2/session/{id}/end |
variable shows 3.0 instead of 3 |
number variables are stored as decimals |
400 session:49 when skipping |
the skip time (solutions.skip.after, milliseconds) has not passed yet |