]> BookStack Code Mirror - bookstack/blob - resources/views/api-docs/index.blade.php
Merge branch 'master' of git://github.com/Binternet/BookStack into Binternet-master
[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                 <div class="sticky-sidebar">
11                     <p class="text-uppercase text-muted mb-xm mt-l"><strong>Getting Started</strong></p>
12
13                     <div class="text-mono">
14                         <div class="mb-xs"><a href="#authentication">Authentication</a></div>
15                         <div class="mb-xs"><a href="#request-format">Request Format</a></div>
16                         <div class="mb-xs"><a href="#listing-endpoints">Listing Endpoints</a></div>
17                         <div class="mb-xs"><a href="#error-handling">Error Handling</a></div>
18                     </div>
19
20                     @foreach($docs as $model => $endpoints)
21                         <p class="text-uppercase text-muted mb-xm mt-l"><strong>{{ $model }}</strong></p>
22
23                         @foreach($endpoints as $endpoint)
24                             <div class="mb-xs">
25                                 <a href="#{{ $endpoint['name'] }}" class="text-mono inline block mr-s">
26                                     <span class="api-method" data-method="{{ $endpoint['method'] }}">{{ $endpoint['method'] }}</span>
27                                 </a>
28                                 <a href="#{{ $endpoint['name'] }}" class="text-mono">
29                                     {{ $endpoint['controller_method'] }}
30                                 </a>
31                             </div>
32                         @endforeach
33                     @endforeach
34                 </div>
35             </div>
36
37             <div style="overflow: auto;">
38
39                 <section code-highlighter class="card content-wrap auto-height">
40                     <h1 class="list-heading text-capitals mb-l">Getting Started</h1>
41
42                     <h5 id="authentication" class="text-mono mb-m">Authentication</h5>
43                     <p>
44                         To access the API a user has to have the <em>"Access System API"</em> permission enabled on one of their assigned roles.
45                         Permissions to content accessed via the API is limited by the roles & permissions assigned to the user that's used to access the API.
46                     </p>
47                     <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>
48                     <pre><code class="language-css">Authorization: Token &lt;token_id&gt;:&lt;token_secret&gt;</code></pre>
49                     <p>Here's an example of an authorized cURL request to list books in the system:</p>
50                     <pre><code class="language-shell">curl --request GET \
51   --url https://example.com/api/books \
52   --header 'Authorization: Token C6mdvEQTGnebsmVn3sFNeeuelGEBjyQp:NOvD3VlzuSVuBPNaf1xWHmy7nIRlaj22'</code></pre>
53                     <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>
54
55                     <hr>
56
57                     <h5 id="request-format" class="text-mono mb-m">Request Format</h5>
58                     <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>
59                     <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>
60
61                     <hr>
62
63                     <h5 id="listing-endpoints" class="text-mono mb-m">Listing Endpoints</h5>
64                     <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>
65                     <pre><code class="language-json">{
66   "data": [
67     {
68       "id": 1,
69       "name": "BookStack User Guide",
70       "slug": "bookstack-user-guide",
71       "description": "This is a general guide on using BookStack on a day-to-day basis.",
72       "created_at": "2019-05-05 21:48:46",
73       "updated_at": "2019-12-11 20:57:31",
74       "created_by": 1,
75       "updated_by": 1,
76       "image_id": 3
77     }
78   ],
79   "total": 16
80 }</code></pre>
81                     <p>
82                         There are a number of standard URL parameters that can be supplied to manipulate and page through the results returned from a listing endpoint:
83                     </p>
84                     <table class="table">
85                         <tr>
86                             <th>Parameter</th>
87                             <th>Details</th>
88                             <th width="30%">Examples</th>
89                         </tr>
90                         <tr>
91                             <td>count</td>
92                             <td>
93                                 Specify how many records will be returned in the response. <br>
94                                 (Default: {{ config('api.default_item_count') }}, Max: {{ config('api.max_item_count') }})
95                             </td>
96                             <td>Limit the count to 50<br><code>?count=50</code></td>
97                         </tr>
98                         <tr>
99                             <td>offset</td>
100                             <td>
101                                 Specify how many records to skip over in the response. <br>
102                                 (Default: 0)
103                             </td>
104                             <td>Skip over the first 100 records<br><code>?offset=100</code></td>
105                         </tr>
106                         <tr>
107                             <td>sort</td>
108                             <td>
109                                 Specify what field is used to sort the data and the direction of the sort (Ascending or Descending).<br>
110                                 Value is the name of a field, A <code>+</code> or <code>-</code> prefix dictates ordering. <br>
111                                 Direction defaults to ascending. <br>
112                                 Can use most fields shown in the response.
113                             </td>
114                             <td>
115                                 Sort by name ascending<br><code>?sort=+name</code> <br> <br>
116                                 Sort by "Created At" date descending<br><code>?sort=-created_at</code>
117                             </td>
118                         </tr>
119                         <tr>
120                             <td>filter[&lt;field&gt;]</td>
121                             <td>
122                                 Specify a filter to be applied to the query. Can use most fields shown in the response. <br>
123                                 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>
124                                 <table>
125                                     <tr>
126                                         <td>eq</td>
127                                         <td>Where <code>&lt;field&gt;</code> equals the filter value.</td>
128                                     </tr>
129                                     <tr>
130                                         <td>ne</td>
131                                         <td>Where <code>&lt;field&gt;</code> does not equal the filter value.</td>
132                                     </tr>
133                                     <tr>
134                                         <td>gt</td>
135                                         <td>Where <code>&lt;field&gt;</code> is greater than the filter value.</td>
136                                     </tr>
137                                     <tr>
138                                         <td>lt</td>
139                                         <td>Where <code>&lt;field&gt;</code> is less than the filter value.</td>
140                                     </tr>
141                                     <tr>
142                                         <td>gte</td>
143                                         <td>Where <code>&lt;field&gt;</code> is greater than or equal to the filter value.</td>
144                                     </tr>
145                                     <tr>
146                                         <td>lte</td>
147                                         <td>Where <code>&lt;field&gt;</code> is less than or equal to the filter value.</td>
148                                     </tr>
149                                     <tr>
150                                         <td>like</td>
151                                         <td>
152                                             Where <code>&lt;field&gt;</code> is "like" the filter value. <br>
153                                             <code>%</code> symbols can be used as wildcards.
154                                         </td>
155                                     </tr>
156                                 </table>
157                             </td>
158                             <td>
159                                 Filter where id is 5: <br><code>?filter[id]=5</code><br><br>
160                                 Filter where id is not 5: <br><code>?filter[id:ne]=5</code><br><br>
161                                 Filter where name contains "cat": <br><code>?filter[name:like]=%cat%</code><br><br>
162                                 Filter where created after 2020-01-01: <br><code>?filter[created_at:gt]=2020-01-01</code>
163                             </td>
164                         </tr>
165                     </table>
166
167                     <hr>
168
169                     <h5 id="error-handling" class="text-mono mb-m">Error Handling</h5>
170                     <p>
171                         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.
172                     </p>
173
174                     <pre><code class="language-json">{
175         "error": {
176                 "code": 401,
177                 "message": "No authorization token found on the request"
178         }
179 }
180 </code></pre>
181
182                 </section>
183
184                 @foreach($docs as $model => $endpoints)
185                     <section class="card content-wrap auto-height">
186                         <h1 class="list-heading text-capitals">{{ $model }}</h1>
187
188                         @foreach($endpoints as $endpoint)
189                             <h6 class="text-uppercase text-muted float right">{{ $endpoint['controller_method'] }}</h6>
190                             <h5 id="{{ $endpoint['name'] }}" class="text-mono mb-m">
191                                 <span class="api-method" data-method="{{ $endpoint['method'] }}">{{ $endpoint['method'] }}</span>
192                                 {{ url($endpoint['uri']) }}
193                             </h5>
194                             <p class="mb-m">{{ $endpoint['description'] ?? '' }}</p>
195                             @if($endpoint['body_params'] ?? false)
196                                 <details class="mb-m">
197                                     <summary class="text-muted">Body Parameters</summary>
198                                     <table class="table">
199                                         <tr>
200                                             <th>Param Name</th>
201                                             <th>Value Rules</th>
202                                         </tr>
203                                         @foreach($endpoint['body_params'] as $paramName => $rules)
204                                         <tr>
205                                             <td>{{ $paramName }}</td>
206                                             <td>
207                                                 @foreach($rules as $rule)
208                                                     <code class="mr-xs">{{ $rule }}</code>
209                                                 @endforeach
210                                             </td>
211                                         </tr>
212                                         @endforeach
213                                     </table>
214                                 </details>
215                             @endif
216                             @if($endpoint['example_request'] ?? false)
217                                 <details details-highlighter class="mb-m">
218                                     <summary class="text-muted">Example Request</summary>
219                                     <pre><code class="language-json">{{ $endpoint['example_request'] }}</code></pre>
220                                 </details>
221                             @endif
222                             @if($endpoint['example_response'] ?? false)
223                                 <details details-highlighter class="mb-m">
224                                     <summary class="text-muted">Example Response</summary>
225                                     <pre><code class="language-json">{{ $endpoint['example_response'] }}</code></pre>
226                                 </details>
227                             @endif
228                             @if(!$loop->last)
229                             <hr>
230                             @endif
231                         @endforeach
232                     </section>
233                 @endforeach
234             </div>
235
236         </div>
237
238
239     </div>
240 @stop