Cliquet provides a basic component to build resource oriented APIs. In most cases, the main customization consists in defining the schema of the records for this resource.

Full example

import colander

from cliquet import resource
from cliquet import schema
from cliquet import utils

class BookmarkSchema(resource.ResourceSchema):
    url = schema.URL()
    title = colander.SchemaNode(colander.String())
    favorite = colander.SchemaNode(colander.Boolean(), missing=False)
    device = colander.SchemaNode(colander.String(), missing='')

    class Options:
        readonly_fields = ('device',)
        unique_fields = ('url',)

class Bookmark(resource.BaseResource):
    mapping = BookmarkSchema()

    def process_record(self, new, old=None):
        if new['device'] != old['device']:
            new['device'] = self.request.headers.get('User-Agent')

        return new

See the ReadingList and Kinto projects source code for real use cases.

Resource Schema

Override the base schema to add extra fields using the Colander API.

class Movie(ResourceSchema):
    director = colander.SchemaNode(colander.String())
    year = colander.SchemaNode(colander.Int(),
    genre = colander.SchemaNode(colander.String(),
                                validator=colander.OneOf(['Sci-Fi', 'Comedy']))

Resource class

In order to customize the resource URLs or behaviour on record processing, the resource class can be extended:

Interaction with storage

In order to customize the interaction of a HTTP resource with its storage, a custom collection can be plugged-in:

from cliquet import resource

class TrackedCollection(resource.Collection):
    def create_record(self, record, parent_id=None, unique_fields=None):
        record = super(TrackedCollection, self).create_record(record,
        trackid = index.track(record)
        record['trackid'] = trackid
        return record

class Payment(resource.BaseResource):
    def __init__(request):
        super(Payment, self).__init__(request)
        self.collection = TrackedCollection(

Custom record ids

By default, records ids are UUID4 <>_.

A custom record ID generator can be set globally in Configuration, or at the resource level:

from cliquet import resource
from cliquet import utils
from import generators

class MsecId(generators.Generator):
    def __call__(self):
        return '%s' % utils.msec_time()

class Mushroom(resource.BaseResource):
    def __init__(request):
        super(Mushroom, self).__init__(request)
        self.collection.id_generator = MsecId()

Generators objects

Custom Usage

Within views

In views, a request object is available and allows to use the storage configured in the application:

from cliquet import resource

def view(request):
    registry = request.registry

    flowers = resource.Collection(,

    flowers.create_record({'name': 'Jonquille', 'size': 30})
    flowers.create_record({'name': 'Amapola', 'size': 18})

    min_size = resource.Filter('size', 20, resource.COMPARISON.MIN)
    records, total = flowers.get_records(filters=[min_size])


Outside views

Outside views, an application context has to be built from scratch.

As an example, let’s build a code that will copy a remote Kinto collection into a local storage:

from cliquet import resource, DEFAULT_SETTINGS
from pyramid import Configurator

config_local = Configurator(settings=DEFAULT_SETTINGS)
    'cliquet.storage_backend': ''
    'cliquet.storage_url': 'postgres://user:pass@db.server.lan:5432/dbname'
local = resource.Collection(,

config_remote = Configurator(settings=DEFAULT_SETTINGS)
    'cliquet.storage_backend': '',
    'cliquet.storage_url': ''
remote = resource.Collection(,
                             auth='Basic bWF0Og==')

records, total = in remote.get_records():
for record in records: