@extends('simple-layout') @section('body')

Getting Started

Authentication

To access the API a user has to have the "Access System API" permission enabled on one of their assigned roles. Permissions to content accessed via the API is limited by the roles & permissions assigned to the user that's used to access the API.

Authentication to use the API is primarily done using API Tokens. Once the "Access System API" 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:

Authorization: Token <token_id>:<token_secret>

Here's an example of an authorized cURL request to list books in the system:

curl --request GET \
  --url https://example.com/api/books \
  --header 'Authorization: Token C6mdvEQTGnebsmVn3sFNeeuelGEBjyQp:NOvD3VlzuSVuBPNaf1xWHmy7nIRlaj22'

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.


Request Format

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 application/x-www-form-urlencoded request data is also accepted. Endpoints that receive file data will need data sent in a multipart/form-data format although this will be highlighted in the documentation for such endpoints.

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.


Listing Endpoints

Some endpoints will return a list of data models. These endpoints will return an array of the model data under a data property along with a numeric total property to indicate the total number of records found for the query within the system. Here's an example of a listing response:

{
  "data": [
    {
      "id": 1,
      "name": "BookStack User Guide",
      "slug": "bookstack-user-guide",
      "description": "This is a general guide on using BookStack on a day-to-day basis.",
      "created_at": "2019-05-05 21:48:46",
      "updated_at": "2019-12-11 20:57:31",
      "created_by": 1,
      "updated_by": 1,
      "image_id": 3
    }
  ],
  "total": 16
}

There are a number of standard URL parameters that can be supplied to manipulate and page through the results returned from a listing endpoint:

Parameter Details Examples
count Specify how many records will be returned in the response.
(Default: {{ config('api.default_item_count') }}, Max: {{ config('api.max_item_count') }})
Limit the count to 50
?count=50
offset Specify how many records to skip over in the response.
(Default: 0)
Skip over the first 100 records
?offset=100
sort Specify what field is used to sort the data and the direction of the sort (Ascending or Descending).
Value is the name of a field, A + or - prefix dictates ordering.
Direction defaults to ascending.
Can use most fields shown in the response.
Sort by name ascending
?sort=+name

Sort by "Created At" date descending
?sort=-created_at
filter[<field>] Specify a filter to be applied to the query. Can use most fields shown in the response.
By default a filter will apply a "where equals" query but the below operations are available using the format filter[<field>:<operation>]
eq Where <field> equals the filter value.
ne Where <field> does not equal the filter value.
gt Where <field> is greater than the filter value.
lt Where <field> is less than the filter value.
gte Where <field> is greater than or equal to the filter value.
lte Where <field> is less than or equal to the filter value.
like Where <field> is "like" the filter value.
% symbols can be used as wildcards.
Filter where id is 5:
?filter[id]=5

Filter where id is not 5:
?filter[id:ne]=5

Filter where name contains "cat":
?filter[name:like]=%cat%

Filter where created after 2020-01-01:
?filter[created_at:gt]=2020-01-01

Error Handling

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.

{
	"error": {
		"code": 401,
		"message": "No authorization token found on the request"
	}
}
@foreach($docs as $model => $endpoints)

{{ $model }}

@foreach($endpoints as $endpoint)
{{ $endpoint['controller_method_kebab'] }}
{{ $endpoint['method'] }} {{ url($endpoint['uri']) }}

{{ $endpoint['description'] ?? '' }}

@if($endpoint['body_params'] ?? false)
Body Parameters @foreach($endpoint['body_params'] as $paramName => $rules) @endforeach
Param Name Value Rules
{{ $paramName }} @foreach($rules as $rule) {{ $rule }} @endforeach
@endif @if($endpoint['example_request'] ?? false)
Example Request
{{ $endpoint['example_request'] }}
@endif @if($endpoint['example_response'] ?? false)
Example Response
{{ $endpoint['example_response'] }}
@endif @if(!$loop->last)
@endif @endforeach
@endforeach
@stop