]> BookStack Code Mirror - bookstack/blob - resources/views/api-docs/index.blade.php
Added configurable API throttling, Handled API errors standardly
[bookstack] / resources / views / api-docs / index.blade.php
1 @extends('simple-layout')
2
3 @section('body')
4
5     <div class="container pt-xl">
6
7         <div class="grid right-focus reverse-collapse">
8             <div>
9
10                 <p class="text-uppercase text-muted mb-xm mt-l"><strong>Getting Started</strong></p>
11
12                 <div class="text-mono">
13                     <div class="mb-xs"><a href="#authentication">Authentication</a></div>
14                     <div class="mb-xs"><a href="#request-format">Request Format</a></div>
15                     <div class="mb-xs"><a href="#listing-endpoints">Listing Endpoints</a></div>
16                     <div class="mb-xs"><a href="#error-handling">Error Handling</a></div>
17                 </div>
18
19                 @foreach($docs as $model => $endpoints)
20                     <p class="text-uppercase text-muted mb-xm mt-l"><strong>{{ $model }}</strong></p>
21
22                     @foreach($endpoints as $endpoint)
23                         <div class="mb-xs">
24                             <a href="#{{ $endpoint['name'] }}" class="text-mono inline block mr-s">
25                                 <span class="api-method" data-method="{{ $endpoint['method'] }}">{{ $endpoint['method'] }}</span>
26                             </a>
27                             <a href="#{{ $endpoint['name'] }}" class="text-mono">
28                                 {{ $endpoint['controller_method'] }}
29                             </a>
30                         </div>
31                     @endforeach
32                 @endforeach
33             </div>
34
35             <div style="overflow: auto;">
36
37                 <section code-highlighter class="card content-wrap auto-height">
38                     <h1 class="list-heading text-capitals mb-l">Getting Started</h1>
39
40                     <h5 id="authentication" class="text-mono mb-m">Authentication</h5>
41                     <p>
42                         To access the API a user has to have the <em>"Access System API"</em> permission enabled on one of their assigned roles.
43                         Permissions to content accessed via the API is limited by the roles & permissions assigned to the user that's used to access the API.
44                     </p>
45                     <p>Authentication to use the API is primarily done using API Tokens. Once the <em>"Access System API"</em> permission has been assigned to a user, a "API Tokens" section should be visible when editing their user profile. Choose "Create Token" and enter an appropriate name and expiry date, relevant for your API usage then press "Save". A "Token ID" and "Token Secret" will be immediately displayed. These values should be used as a header in API HTTP requests in the following format:</p>
46                     <pre><code class="language-css">Authorization: Token &lt;token_id&gt;:&lt;token_secret&gt;</code></pre>
47                     <p>Here's an example of an authorized cURL request to list books in the system:</p>
48                     <pre><code class="language-shell">curl --request GET \
49   --url https://example.com/api/books \
50   --header 'Authorization: Token C6mdvEQTGnebsmVn3sFNeeuelGEBjyQp:NOvD3VlzuSVuBPNaf1xWHmy7nIRlaj22'</code></pre>
51                     <p>If already logged into the system within the browser, via a user account with permission to access the API, the system will also accept an existing session meaning you can browse API endpoints directly in the browser or use the browser devtools to play with the API.</p>
52
53                     <hr>
54
55                     <h5 id="request-format" class="text-mono mb-m">Request Format</h5>
56                     <p>The API is primarily design to be interfaced using JSON so the majority of API endpoints, that accept data, will read JSON request data although <code>application/x-www-form-urlencoded</code> request data is also accepted. Endpoints that receive file data will need data sent in a <code>multipart/form-data</code> format although this will be highlighted in the documentation for such endpoints.</p>
57                     <p>For endpoints in this documentation that accept data, a "Body Parameters" table will be available showing the parameters that will accepted in the request. Any rules for the values of such parameters, such as the data-type or if they're required, will be shown alongside the parameter name.</p>
58
59                     <hr>
60
61                     <h5 id="listing-endpoints" class="text-mono mb-m">Listing Endpoints</h5>
62                     <p>Some endpoints will return a list of data models. These endpoints will return an array of the model data under a <code>data</code> property along with a numeric <code>total</code> property to indicate the total number of records found for the query within the system. Here's an example of a listing response:</p>
63                     <pre><code class="language-json">{
64   "data": [
65     {
66       "id": 1,
67       "name": "BookStack User Guide",
68       "slug": "bookstack-user-guide",
69       "description": "This is a general guide on using BookStack on a day-to-day basis.",
70       "created_at": "2019-05-05 21:48:46",
71       "updated_at": "2019-12-11 20:57:31",
72       "created_by": 1,
73       "updated_by": 1,
74       "image_id": 3
75     }
76   ],
77   "total": 16
78 }</code></pre>
79                     <p>
80                         There are a number of standard URL parameters that can be supplied to manipulate and page through the results returned from a listing endpoint:
81                     </p>
82                     <table class="table">
83                         <tr>
84                             <th>Parameter</th>
85                             <th>Details</th>
86                             <th width="30%">Examples</th>
87                         </tr>
88                         <tr>
89                             <td>count</td>
90                             <td>
91                                 Specify how many records will be returned in the response. <br>
92                                 (Default: {{ config('api.default_item_count') }}, Max: {{ config('api.max_item_count') }})
93                             </td>
94                             <td>Limit the count to 50<br><code>?count=50</code></td>
95                         </tr>
96                         <tr>
97                             <td>offset</td>
98                             <td>
99                                 Specify how many records to skip over in the response. <br>
100                                 (Default: 0)
101                             </td>
102                             <td>Skip over the first 100 records<br><code>?offset=100</code></td>
103                         </tr>
104                         <tr>
105                             <td>sort</td>
106                             <td>
107                                 Specify what field is used to sort the data and the direction of the sort (Ascending or Descending).<br>
108                                 Value is the name of a field, A <code>+</code> or <code>-</code> prefix dictates ordering. <br>
109                                 Direction defaults to ascending. <br>
110                                 Can use most fields shown in the response.
111                             </td>
112                             <td>
113                                 Sort by name ascending<br><code>?sort=+name</code> <br> <br>
114                                 Sort by "Created At" date descending<br><code>?sort=-created_at</code>
115                             </td>
116                         </tr>
117                         <tr>
118                             <td>filter[&lt;field&gt;]</td>
119                             <td>
120                                 Specify a filter to be applied to the query. Can use most fields shown in the response. <br>
121                                 By default a filter will apply a "where equals" query but the below operations are available using the format filter[&lt;field&gt;:&lt;operation&gt;] <br>
122                                 <table>
123                                     <tr>
124                                         <td>eq</td>
125                                         <td>Where <code>&lt;field&gt;</code> equals the filter value.</td>
126                                     </tr>
127                                     <tr>
128                                         <td>ne</td>
129                                         <td>Where <code>&lt;field&gt;</code> does not equal the filter value.</td>
130                                     </tr>
131                                     <tr>
132                                         <td>gt</td>
133                                         <td>Where <code>&lt;field&gt;</code> is greater than the filter value.</td>
134                                     </tr>
135                                     <tr>
136                                         <td>lt</td>
137                                         <td>Where <code>&lt;field&gt;</code> is less than the filter value.</td>
138                                     </tr>
139                                     <tr>
140                                         <td>gte</td>
141                                         <td>Where <code>&lt;field&gt;</code> is greater than or equal to the filter value.</td>
142                                     </tr>
143                                     <tr>
144                                         <td>lte</td>
145                                         <td>Where <code>&lt;field&gt;</code> is less than or equal to the filter value.</td>
146                                     </tr>
147                                     <tr>
148                                         <td>like</td>
149                                         <td>
150                                             Where <code>&lt;field&gt;</code> is "like" the filter value. <br>
151                                             <code>%</code> symbols can be used as wildcards.
152                                         </td>
153                                     </tr>
154                                 </table>
155                             </td>
156                             <td>
157                                 Filter where id is 5: <br><code>?filter[id]=5</code><br><br>
158                                 Filter where id is not 5: <br><code>?filter[id:ne]=5</code><br><br>
159                                 Filter where name contains "cat": <br><code>?filter[name:like]=%cat%</code><br><br>
160                                 Filter where created after 2020-01-01: <br><code>?filter[created_at:gt]=2020-01-01</code>
161                             </td>
162                         </tr>
163                     </table>
164
165                     <hr>
166
167                     <h5 id="error-handling" class="text-mono mb-m">Error Handling</h5>
168                     <p>
169                         Successful responses will return a 200 or 204 HTTP response code. Errors will return a 4xx or a 5xx HTTP response code depending on the type of error. Errors follow a standard format as shown below. The message provided may be translated depending on the configured language of the system in addition to the API users' language preference. The code provided in the JSON response will match the HTTP response code.
170                     </p>
171
172                     <pre><code class="language-json">{
173         "error": {
174                 "code": 401,
175                 "message": "No authorization token found on the request"
176         }
177 }
178 </code></pre>
179
180                 </section>
181
182                 @foreach($docs as $model => $endpoints)
183                     <section class="card content-wrap auto-height">
184                         <h1 class="list-heading text-capitals">{{ $model }}</h1>
185
186                         @foreach($endpoints as $endpoint)
187                             <h6 class="text-uppercase text-muted float right">{{ $endpoint['controller_method'] }}</h6>
188                             <h5 id="{{ $endpoint['name'] }}" class="text-mono mb-m">
189                                 <span class="api-method" data-method="{{ $endpoint['method'] }}">{{ $endpoint['method'] }}</span>
190                                 {{ url($endpoint['uri']) }}
191                             </h5>
192                             <p class="mb-m">{{ $endpoint['description'] ?? '' }}</p>
193                             @if($endpoint['body_params'] ?? false)
194                                 <details class="mb-m">
195                                     <summary class="text-muted">Body Parameters</summary>
196                                     <table class="table">
197                                         <tr>
198                                             <th>Param Name</th>
199                                             <th>Value Rules</th>
200                                         </tr>
201                                         @foreach($endpoint['body_params'] as $paramName => $rules)
202                                         <tr>
203                                             <td>{{ $paramName }}</td>
204                                             <td>
205                                                 @foreach($rules as $rule)
206                                                     <code class="mr-xs">{{ $rule }}</code>
207                                                 @endforeach
208                                             </td>
209                                         </tr>
210                                         @endforeach
211                                     </table>
212                                 </details>
213                             @endif
214                             @if($endpoint['example_request'] ?? false)
215                                 <details details-highlighter class="mb-m">
216                                     <summary class="text-muted">Example Request</summary>
217                                     <pre><code class="language-json">{{ $endpoint['example_request'] }}</code></pre>
218                                 </details>
219                             @endif
220                             @if($endpoint['example_response'] ?? false)
221                                 <details details-highlighter class="mb-m">
222                                     <summary class="text-muted">Example Response</summary>
223                                     <pre><code class="language-json">{{ $endpoint['example_response'] }}</code></pre>
224                                 </details>
225                             @endif
226                             @if(!$loop->last)
227                             <hr>
228                             @endif
229                         @endforeach
230                     </section>
231                 @endforeach
232             </div>
233
234         </div>
235
236
237     </div>
238 @stop