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
crystal init app notes
cd notesAdd Runcobo to shard.yml and install it:
dependencies:
runcobo:
github: runcobo/runcobo
version: ~> 3.1shards install2. Write the application
Replace the contents of src/notes.cr:
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.startA few things to notice:
- Each endpoint is a class.
post "/notes"andget "/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 ofcalldoesn't run.- After that line,
input.titleis aStringandinput.bodyis aString?— 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
crystal run src/notes.cr... INFO - runcobo: Runcobo 3.1.4 is listening on 0.0.0.0:30004. Try it
Create a note:
curl -i localhost:3000/notes \
-H 'Content-Type: application/json' \
-d '{"title":"Hello","body":"First note"}'HTTP/1.1 201 Created
X-Request-Id: 3f0c2d1e-...
Content-Type: application/json
{"id":1,"title":"Hello","body":"First note"}List and fetch:
curl localhost:3000/notes
# [{"id":1,"title":"Hello"}]
curl localhost:3000/notes/1
# {"id":1,"title":"Hello","body":"First note"}5. Send something invalid
curl -i localhost:3000/notes \
-H 'Content-Type: application/json' \
-d '{"title":""}'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:
| Request | Status | Error code |
|---|---|---|
GET /notes/abc | 422 | invalid_type on ["id"] — the path parameter isn't a number |
{"title": 42} | 422 | invalid_type — a JSON number is not converted to a string |
{} | 422 | missing on ["title"] |
body {oops | 400 | invalid_json |
no Content-Type header | 415 | unsupported_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
- See how a full application is organized: Project Structure
- Store data in PostgreSQL, MySQL or SQLite: Database Access
- Learn everything an action can do: Actions
- Go deeper on validation: Request Contracts