|
| 1 | +# Public API |
| 2 | + |
| 3 | +This API provides a way to programmatically import data into the p5.js Web Editor. |
| 4 | + |
| 5 | +# Authentication |
| 6 | + |
| 7 | +Access to the API is available via a Personal Access Token, linked to an existing editor user account. Tokens can be created and deleted via logged-in user’s Settings page. |
| 8 | + |
| 9 | +When contacting the API, the username and token must be sent with every request using basic auth. |
| 10 | + |
| 11 | +This involved sending the base64 encoded `${username}:${personalAccessToken}` in the `Authorization` header. For example: |
| 12 | + `Authorization: Basic cDU6YWJjMTIzYWJj` |
| 13 | + |
| 14 | +# API Access |
| 15 | + |
| 16 | +- All requests send and receive `Content-Type: application/json` unless otherwise stated |
| 17 | + |
| 18 | +# Versioning |
| 19 | + |
| 20 | +The API is versioned and this version is indicated in the root URL path e.g. version 1 of the API can be found at `http://editor.p5js.org/api/v1`. |
| 21 | + |
| 22 | +You must provide the version number when accessing the API. |
| 23 | + |
| 24 | +| Version | Release date | |
| 25 | +| ------- | ------------ | |
| 26 | +| v1 | Unreleased | |
| 27 | + |
| 28 | +# Models |
| 29 | + |
| 30 | +The API accepts and returns the following model objects, as JSON. |
| 31 | + |
| 32 | +## Sketch |
| 33 | + |
| 34 | +| Name | Type | Description | |
| 35 | +| ----- | ----------------- | ------------------------------------------------------------------------------------ | |
| 36 | +| name | String | The sketch’s title | |
| 37 | +| files | DirectoryContents | The files and directories in this sketch. See `DirectoryContents` for the structure. | |
| 38 | +| slug | String | A path that can be used to access the sketch | |
| 39 | + |
| 40 | + |
| 41 | + { |
| 42 | + "id": String, // opaque ID |
| 43 | + "name: String, |
| 44 | + "files": DirectoryContents, |
| 45 | + "slug": String // optional |
| 46 | + } |
| 47 | + |
| 48 | +### Validations |
| 49 | + |
| 50 | +- `files` must have exactly one top-level file with the `.html` extension. If none is provided, then a default `index.html` and associated `style.css` will be automatically created. |
| 51 | +- `slug` must be an URL-safe string |
| 52 | +- `slug` must be unique across all user's sketches |
| 53 | + |
| 54 | +## DirectoryContents |
| 55 | + |
| 56 | +A map of filenames to `File` or `Directory`. The key of each item is used as the filename. Using a map ensures that filenames are unique in the directory. |
| 57 | + |
| 58 | + |
| 59 | + { |
| 60 | + [String]: File | Directory |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + { |
| 65 | + "sketch.js": { "content": "var answer = 42;" }, |
| 66 | + "index.html" { "content": "..." } |
| 67 | + } |
| 68 | + |
| 69 | +## DirectFile |
| 70 | + |
| 71 | +This file is editable in the Editor UI and stored in the Editor's database. |
| 72 | + |
| 73 | +| Name | Type | Description | |
| 74 | +| ------- | ------------ | ------------------------------------------ | |
| 75 | +| content | UTF-8 String | The contents of the file as a UTF-8 string | |
| 76 | + |
| 77 | + { |
| 78 | + "content": String |
| 79 | + } |
| 80 | + |
| 81 | +## ReferencedFile |
| 82 | + |
| 83 | +This file is hosted elsewhere on the Internet. It appears in the Editor's listing and can be referenced using a proxy URL in the Editor. |
| 84 | + |
| 85 | + |
| 86 | +| Name | Type | Description | |
| 87 | +| ---- | ---- | ----------------------------------------------- | |
| 88 | +| url | URL | A valid URL pointing to a file hosted elsewhere | |
| 89 | + |
| 90 | + { |
| 91 | + "url": URL |
| 92 | + } |
| 93 | + |
| 94 | +## File |
| 95 | + |
| 96 | +A `File` is either a `DirectFile` or `ReferencedFile`. The API supports both everywhere. |
| 97 | + |
| 98 | +## Directory |
| 99 | + |
| 100 | +| Name | Type | Description | |
| 101 | +| ----- | ----------------- | ------------------------------- | |
| 102 | +| files | DirectoryContents | A map of the directory contents | |
| 103 | + |
| 104 | + { |
| 105 | + "files": DirectoryContents |
| 106 | + } |
| 107 | + |
| 108 | +# API endpoints |
| 109 | + |
| 110 | +## Sketches |
| 111 | + |
| 112 | +## `GET /:user/sketches` |
| 113 | + |
| 114 | +List a user’s sketches. |
| 115 | + |
| 116 | +This will not return the files within the sketch, just the sketch metadata. |
| 117 | + |
| 118 | +### Request format |
| 119 | +No body. |
| 120 | + |
| 121 | +### Response format |
| 122 | + { |
| 123 | + "sketches": Array<Sketch> |
| 124 | + } |
| 125 | + |
| 126 | +### Example |
| 127 | + |
| 128 | + GET /p5/sketches |
| 129 | + |
| 130 | + { |
| 131 | + "sketches": [ |
| 132 | + { "id": "H1PLJg8_", "name": "My Lovely Sketch" }, |
| 133 | + { "id": "Bkhf0APpg", "name": "My Lovely Sketch 2" } |
| 134 | + ] |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +## `POST /:user/sketches` |
| 139 | + |
| 140 | +Create a new sketch. |
| 141 | + |
| 142 | +A sketch must contain at least one file with the `.html` extension. If none if provided in the payload, a default `index.html` and linked `style.css` file will be created automatically. |
| 143 | + |
| 144 | +### Request format |
| 145 | +See `Sketch` in Models above. |
| 146 | + |
| 147 | +### Response format |
| 148 | + { |
| 149 | + "id": String |
| 150 | + } |
| 151 | + |
| 152 | +### Example |
| 153 | + |
| 154 | + POST /p5/sketches |
| 155 | + |
| 156 | + { |
| 157 | + "name": "My Lovely Sketch", |
| 158 | + "files": { |
| 159 | + "index.html": { "content": "<DOCTYPE html!><body>Hello!</body></html>" }, |
| 160 | + "sketch.js": { "content": "var useless = true;" } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +`files` can be nested to represent a folder structure. For example, this will create an empty “data” directory in the sketch: |
| 165 | + |
| 166 | + |
| 167 | + POST /p5/sketches |
| 168 | + |
| 169 | + { |
| 170 | + "name": "My Lovely Sketch 2", |
| 171 | + "files": [ |
| 172 | + { |
| 173 | + "name": "assets", |
| 174 | + "type": "", |
| 175 | + "files": { |
| 176 | + "index.html": { "content": "<DOCTYPE html!><body>Hello!</body></html>" }, |
| 177 | + "data": { |
| 178 | + "files": {} |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +### Responses |
| 185 | + |
| 186 | +| HTTP code | Body | |
| 187 | +| ------------------------ | ----------------------------------------------------------------- | |
| 188 | +| 201 Created | id of sketch | |
| 189 | +| 422 Unprocessable Entity | file validation failed, unsupported filetype, slug already exists | |
| 190 | + |
| 191 | + |
| 192 | +### Examples |
| 193 | + |
| 194 | + 201 CREATED |
| 195 | + |
| 196 | + { |
| 197 | + "id": "Ckhf0APpg" |
| 198 | + } |
| 199 | + |
| 200 | +## `DELETE /:user/sketches/:id` |
| 201 | + |
| 202 | +Delete a sketch and all it’s associated files. |
| 203 | + |
| 204 | +### Request format |
| 205 | +No body |
| 206 | + |
| 207 | +### Response format |
| 208 | +No body |
| 209 | + |
| 210 | +### Example |
| 211 | + |
| 212 | + DELETE /p5/sketches/Ckhf0APpg |
| 213 | + |
| 214 | +### Responses |
| 215 | + |
| 216 | +| HTTP code | Description | |
| 217 | +| ------------- | ----------------------- | |
| 218 | +| 200 OK | Sketch has been deleted | |
| 219 | +| 404 Not Found | Sketch does not exist | |
0 commit comments