Skip to content

Quick Start ​

In this guide you'll build a tiny notes API in a single file: create a note, list notes, and fetch one by ID. Along the way you'll see routes, request contracts and automatic validation errors. Notes are kept in memory, so there's nothing to set up — Database Access shows the same ideas on PostgreSQL.

You'll need Crystal installed — see Installation.

1. Create the project ​

sh
crystal init app notes
cd notes

Add Runcobo to shard.yml and install it:

shard.yml
yaml
dependencies:
  runcobo:
    github: runcobo/runcobo
    version: ~> 3.1
sh
shards install

2. Write the application ​

Replace the contents of src/notes.cr:

src/notes.cr
crystal
require "runcobo"

# A note kept in memory for this example.
record Note, id : Int64, title : String, body : String?

NOTES = [] of Note

# What a client may send when creating a note.
class CreateNoteContract < RequestValidation::Contract
  schema do
    required :title, String, filled: true, max_size: 100
    optional :body, String, nullable: true
  end
end

# What identifies a note in the URL.
class NotePathContract < RequestValidation::Contract
  schema do
    required :id, Int64
  end
end

class CreateNote < BaseAction
  post "/notes"

  call do
    input = validate(CreateNoteContract)
    note = Note.new(NOTES.size.to_i64 + 1, input.title, input.body)
    NOTES << note
    render_json({id: note.id, title: note.title, body: note.body}, status_code: 201)
  end
end

class ListNotes < BaseAction
  get "/notes"

  call do
    render_json(NOTES.map { |note| {id: note.id, title: note.title} })
  end
end

class ShowNote < BaseAction
  get "/notes/:id"

  call do
    input = validate(NotePathContract)
    if note = NOTES.find { |candidate| candidate.id == input.id }
      render_json({id: note.id, title: note.title, body: note.body})
    else
      render_json({error: "not_found"}, status_code: 404)
    end
  end
end

Runcobo.start

A few things to notice:

  • Each endpoint is a class. post "/notes" and get "/notes/:id" register its routes.
  • validate(CreateNoteContract) checks the request against the contract and returns typed input. If the request doesn't fit, Runcobo responds with an error and the rest of call doesn't run.
  • After that line, input.title is a String and input.body is a String? — the types come from the contract.
  • In a real application, each action, its contract and its template live in their own files — see Project Structure. One file keeps this first example short.

3. Run it ​

sh
crystal run src/notes.cr
text
... INFO - runcobo: Runcobo 3.1.4 is listening on 0.0.0.0:3000

4. Try it ​

Create a note:

sh
curl -i localhost:3000/notes \
  -H 'Content-Type: application/json' \
  -d '{"title":"Hello","body":"First note"}'
http
HTTP/1.1 201 Created
X-Request-Id: 3f0c2d1e-...
Content-Type: application/json

{"id":1,"title":"Hello","body":"First note"}

List and fetch:

sh
curl localhost:3000/notes
# [{"id":1,"title":"Hello"}]

curl localhost:3000/notes/1
# {"id":1,"title":"Hello","body":"First note"}

5. Send something invalid ​

sh
curl -i localhost:3000/notes \
  -H 'Content-Type: application/json' \
  -d '{"title":""}'
http
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json

{"errors":[{"source":"request","path":["title"],"code":"filled","params":{}}]}

The rest of call never ran: validate rejected the request, and Runcobo answered with a structured error that a client can map to a form field. Try a few more:

RequestStatusError code
GET /notes/abc422invalid_type on ["id"] — the path parameter isn't a number
{"title": 42}422invalid_type — a JSON number is not converted to a string
{}422missing on ["title"]
body {oops400invalid_json
no Content-Type header415unsupported_media_type

Auto-restart during development ​

Crystal compiles before it runs, so a change needs a rebuild. For this single file, stop the server with Ctrl-C and run it again. In a full project, bin/cobo dev does it for you: it watches src/, rebuilds on every save and restarts the server — and if a change doesn't compile, the previous version keeps running.

Where to go next ​

Released under the MIT License.