Customizing the ReSTful interface

HTTP methods

By default, the APIManager.create_api() method creates a read-only interface; requests with HTTP methods other than GET will cause a response with 405 Method Not Allowed. To explicitly specify which methods should be allowed for the endpoint, pass a list as the value of keyword argument methods:

apimanager.create_api(Person, methods=['GET', 'POST', 'DELETE'])

This creates an endpoint at /api/person which responds to GET, POST, and DELETE methods, but not to other ones like PUT or PATCH.

The recognized HTTP methods and their semantics are described below (assuming you have created an API for an entity Person). All endpoints which respond with data respond with serialized JSON strings.

GET /api/person

Returns a list of all Person instances.

GET /api/person/(int: id)

Returns a single Person instance with the given id.

GET /api/person?q=<searchjson>

Returns a list of all Person instances which match the search query specified in the query parameter q. For more information on searching, see Making search queries.

DELETE /api/person/(int: id)

Deletes the person with the given id and returns 204 No Content.

POST /api/person

Creates a new person in the database and returns its id. The initial attributes of the Person are read as JSON from the body of the request. For information about the format of this request, see Format of requests and responses.

PATCH /api/person/(int: id)

Updates the attributes of the Person with the given id. The attributes are read as JSON from the body of the request. For information about the format of this request, see Format of requests and responses.

PATCH /api/person

This is only available if the allow_patch_many keyword argument is set to True when calling the create_api() method. For more information, see Enable patching all instances.

Updates the attributes of all Person instances. The attributes are read as JSON from the body of the request. For information about the format of this request, see Format of requests and responses.

PUT /api/person
PUT /api/person/(int: id)

Aliases for PATCH /api/person and PATCH /api/person/(int:id).

API prefix

To create an API at a different prefix, use the url_prefix keyword argument:

apimanager.create_api(Person, url_prefix='/api/v2')

Then your API for Person will be available at /api/v2/person.

Collection name

By default, the name of the collection which appears in the URLs of the API will be the name of the table which backs your model. If your model is a SQLAlchemy model, this will be the value of __tablename__. If your model is a Flask-SQLAlchemy model, this will be the lowercase name of the model with CamelCase changed to camel_case.

To provide a different name for the model, provide a string to the collection_name keyword argument of the APIManager.create_api() method:

apimanager.create_api(Person, collection_name='people')

Then the API will be exposed at /api/people instead of /api/person.

Enable patching all instances

By default, a PATCH /api/person request (note the missing ID) will cause a 405 Method Not Allowed response. By setting the allow_patch_many keyword argument of the APIManager.create_api() method to be True, PATCH /api/person requests will patch the provided attributes on all instances of Person:

apimanager.create_api(Person, allow_patch_many=True)

Capturing validation errors

By default, no validation is performed by Flask-Restless; if you want validation, implement it yourself in your database models. However, by specifying a list of exceptions raised by your backend on validation errors, Flask-Restless will forward messages from raised exceptions to the client in an error response.

A reasonable validation framework you might use for this purpose is SQLAlchemy Validation. You can also use the validates() decorator that comes with SQLAlchemy.

For example, if your validation framework includes an exception called ValidationError, then call the APIManager.create_api() method with the validation_exceptions keyword argument:

from cool_validation_framework import ValidationError
apimanager.create_api(Person, validation_exceptions=[ValidationError])

Note

Currently, Flask-Restless expects that an instance of a specified validation error will have a errors attribute, which is a dictionary mapping field name to error description (note: one error per field). If you have a better, more general solution to this problem, please visit our issue tracker.

Now when you make POST and PATCH requests with invalid fields, the JSON response will look like this:

HTTP/1.1 400 Bad Request

{ "validation_errors":
    {
      "age": "Must be an integer",
    }
}

Currently, Flask-Restless can only forward one exception at a time to the client.

Exposing evaluation of SQL functions

If the allow_functions keyword argument is set to True when creating an API for a model using APIManager.create_api(), then an endpoint will be made available for GET /api/eval/person which responds to requests for evaluation of functions on all instances the model.

For information about the request and response formats for this endpoint, see Function evaluation.

Specifying which columns are provided in responses

By default, all columns of your model will be exposed by the API. If the include_columns keyword argument is an iterable of strings, only columns with those names (that is, the strings represent the names of attributes of the model which are Column objects) will be provided in JSON responses for GET requests.

For example, if your models are defined like this (using Flask-SQLAlchemy):

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    birth_date = db.Column(db.Date)
    computers = db.relationship('Computer')

and you want your JSON responses to include only the values of the name and birth_date columns, create your API with the following arguments:

apimanager.create_api(Person, include_columns=['name', 'birth_date'])

Now requests like GET /api/person/1 will return JSON objects which look like this:

{"name": "Jeffrey", "birth_date": "1999-12-31"}

The exclude_columns keyword argument works similarly; it forces your JSON responses to include only the columns not specified in exclude_columns. For example:

apimanager.create_api(Person, exclude_columns=['name', 'birth_date'])

will produce responses like:

{"id": 1, "computers": [{"id": 1, "vendor": "Apple", "model": "MacBook"}]}

In this example, the Person model has a one-to-many relationship with the Computer model. To specify which columns on the related models will be included or excluded, include a string of the form '<relation>.<column>', where <relation> is the name of the relationship attribute of the model and <column> is the name of the column on the related model which you want to be included or excluded. For example:

includes = ['name', 'birth_date', 'computers', 'computers.vendor']
apimanager.create_api(Person, include_columns=includes)

will produce responses like:

{
  "name": "Jeffrey",
  "birth_date": "1999-12-31",
  "computers": [{"vendor": "Apple"}]
}

An attempt to include a field on a related model without including the relationship field has no effect:

includes = ['name', 'birth_date', 'computers.vendor']
apimanager.create_api(Person, include_columns=includes)
{"name": "Jeffrey", "birth_date": "1999-12-31"}

Requiring authentication for some methods

Note

The authentication system in Flask-Restless is relatively simple, but since I suspect it is a common requirement for ReSTful APIs, suggestions, comments, and pull requests are much appreciated. Please visit our issue tracker.

If you want certain HTTP methods to require authentication, use the authentication_required_for and authentication_function keyword arguments to the APIManager.create_api() method. If you specify the former, you must also specify the latter.

authentication_required_for is the list of HTTP method names which will require authentication and authentication_function is a function with zero arguments which returns True if and only if the client making the request has been authenticated. This function can really be anything you like, but presumably it will have something to do with your authentication framework.

For an example using Flask-Login, see the examples/authentication directory in the source distribution, or view it online at GitHub.

Updating POST parameters before committing

To apply some function to the POST form parameters before the database model is created, specify the post_form_preprocessor keyword. The value of post_form_preprocessor must be a function which accepts a single dictionary as input and outputs a dictionary. The input dictionary is the dictionary mapping names of columns of the model to values to assign to that column, as specified by the JSON provided in the body of the POST request. The output dictionary should be the same, but with whatever additions, deletions, or modifications you wish.

For example, if the client is making a POST request to a model which which has an owner field which should contain the ID of the currently logged in user, you may wish for the server to append the mapping ('owner', current_user.id) to the form parameters. In this case, you would set the value of post_form_processor to be the function defined below:

def add_user_id(dictionary):
    dictionary['owner'] = current_user.id
    return dictionary