Skip to content


Repository files navigation


Dependency Status devDependency Status Build Status

crystal_api is a set of tools to allow create very fast JSON APIs.

Key notes:

  • all models are struct instead of class (like in
  • custom SQL requests are preferred and encouraged
  • but you can use one macro to create all REST (no customization)
  • there are some Rails-like methods (coming soon)
  • logic need to be customized

Sample payment API is explained here

Here, Alive - real life API example with JS frontend

API of API - list of all methods (coming soon)


  • Fix DB mapping to allow create database - add types of columns to definition list
  • Check and fix JSON mapping
  • Update action
  • Destroy action
  • Clean Postgres adapter
  • Rewrite for easier usage as lib
  • JSON response header
  • DB inline config (no config file needed)
  • devise compatible sign in controller
  • JWT request authentication
  • Initial rights managament
  • Utilize singleton-like approach to get service
  • Remove kemal-pg and use crystal-pg
  • Use DB connection pool
  • Use typed queries
  • Escape parameters - only quotation characters
  • Add Nil to field type as union by default - shorter model definition
  • Rename service to something better
  • Add "scope" method to model Mode.scope({where: Hash, limit: Int32, order: String})
  • Add page, per_page, offset
  • Add random to fetch_one
  • Add random order to fetch_all
  • One method for fetching
  • Models should be as much immutable as possible
  • Websockets


Please check:

Fast full REST


# all fields must be union with Nil
crystal_model(EventModel, id : Int32, name : String)

# magic macro
# crystal_resource_full_rest(<name>, <resource path without '/' >, <DB table name>, <Model struct>)
crystal_resource_full_rest(event, events, events, EventModel)

# create table if not exists

# set port
Kemal.config.port = 8002
# run, this run migrations before running the HTTP server

Custom API

Please check short sample. This is money transfers API. User has its account and assigned payments: incoming (external transfered money to system), outgoing (user withdraw money), transfer (user's account money transfered to another user's account).

Rails like model methods

At this moment I'm refactoring and adding code for easier DB operations. Readme will be updated after that moment.


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request



Simple PostgreSQL REST API in Crystal with devise-like auth.







No packages published