# Introduction

Welcome to Vanguard JSON API documentation. It covers all endpoints that you can use to build your mobile, desktop or web applications around Vanguard, from simple username/password authentication to user management.

# HTTP Status Codes

HTTP Status Code Status Message Description
200 OK Everything worked as expected.
201 Created Resource was created successfully.
400 Bad Request The request was unacceptable, often due to missing a required parameter.
401 Unauthorized No valid API key provided.
403 Forbidden Accessing the resource is forbidden for this user.
404 Not Found The requested resource doesn't exist.
422 Unprocessable Entity Required fields are missing or cannot be processed.
500, 502, 503, 504 Server Errors Something went wrong on Vanguard's end.

# Authorization

Vanguard API uses the API key for authorization. You can get the API key by providing valid username/email and password, as it is explained within the login section below.

Any request that requires an authorized user expects this token to be supplied in the form of a Authorization header like so:

Authorization: Bearer {api_key}

When a API key has not been supplied, or the supplied API key is invalid, the API will return a 401 Unauthorized response with the following body:

{
    "message": "Unauthenticated."
}

# Error Handling

The API returns a variety of errors and each error will contain a message property with the info about the error itself. The most common type of errors are the validation errors that will always be formatted like following:

{
    "message": "The given data was invalid.",
    "errors": {
        "first_name": [
            "The first name field is required."
        ],
        "last_name": [
            "The last name field is required."
        ],
        "email": [
            "The email field is required."
        ]
    }
}

# Pagination

Some endpoints will return data in a paginated list. A paginated response will look similar to this:

{
    "data": [
        //...
    ],
    "links": {
        "first": "https://yourwebsite.com/api/users?page=1",
        "last": "https://yourwebsite.com/api/users?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://yourwebsite.com/api/users",
        "per_page": 15,
        "to": 15,
        "total": 15
    }
}

As per the example above, each paginated response will have the data array, which contains all the items from the specific page, a links object with full URLs for fetching first, last, prev and next pages, and the meta object which contains all the info about the resource you are paginating.

Some endpoints allow you to specific number of records that you want to receive per page. To achieve that you can append the per_page=X parameter to the URL of the request. For example, if you want to request 50 users per page, the URL parameters should look like the following:

/users?per_page=50

Pagination limit is set to 100 for most resources which means that you cannot request more than 100 records per page. If you provide a larger number, it will be ignored and 100 records per page will be returned.

To request a different page, just append the page=X parameter to the URL. For our customers example above, you can get the second page like following:

/users?per_page=50&page=2

# Filtering Results

Each section inside the docs that supports filtering will have a list of supported filters defined and you can specify as a query parameter while making the request.

Filters should be provided as a part of the enpoint URL in the following format:

?filter[FILTER_NAME]=VALUE

For example, lets say that you want to get all the users that are named John. It can be done by providing the search filter while making the request:

customers?filter[search]=John

There are two different types of filters, the exact and partial filters.

# Exact Filters

Exact filters will only return those records where a specific attribute is exactly the same as the provided filter value.

For example, if an endpoint supports an exact filter for filtering out the records by status and if you provide ?filter[status]=1 query parameter, only records that have the status attribute set to 1 will be returned.

# Partial Filters

The partial filters are usually used for text based fields an it will return all the records that have the provided filter vaule anywhere within the attribute value.

For example, if there is a filter name defined for the specific resource and you provide the ?filter[name]=one filter within the URL, the API will return all the customers from the db that contain the word one anywhere within their name attribute and not only those who has the name exactly set as one.

# Sorting Results

All sortable resources will provide the list of sortable fields that can be used for sorting the data that is being returned from the API.

To sort a resource by the specific field, just append the ?sort=FIELD query parameter while you make the request to the API endpoint and it will sort the items in ascending order. To sort the items in descending order, prefix the field with - sign: ?sort=-FIELD.

For example, if you want to sort the users in ascending order by the created_at timestamp, you should make a request to the following endpoint:

/users?sort=created_at

If you want to sort them by the created_at timestamp in descending order, the enpoint should be updated to look like the following:

/users?sort=-created_at

Vanguard API provides a convinient way to fetch some related resources while fetching a specific resource. For example, it can be pretty useful to get the info about the user's role while fetching a specific user.

If there are some supported includes for the specific endpoint, they will be provided inside the endpoint description. To specify which additional resources you want to include, just append the include query parameter to the URL, like following:

?include=resource1,resource2,...

For example, if you want to include the details about the user's role while fetching the list of users, your query parameters will look like the following:

/users?include=role

Each user in the list of users returned by the API will now have the additional role object where you can find all the info about the user's role.