Build services Browse docs
On this page 6 sections

Build services

HTTP routes

Declare endpoints, read requests, answer with the right status. Patterns, the req surface, the ok/err split, bodies, and per-route rate budgets.

By the end of this page you can declare a route, read what the caller sent, and answer with the status you want. A service program is a set of routes at the top level. A file with at least one route gets a main generated for it, so you do not write one. The listen port is the first command-line argument (or the file’s port declaration, see below):

Terminal
./orbit build greet.orb -o greet
./greet 8080

If you need startup work, add fn main() -> int; the generated server calls it before accepting connections, and its return value becomes the exit code when it returns. This page uses the key-checked examples/blog_api.orb throughout — every response below is canned from that file, not from a live server.

Declaring routes

A route names a method and a literal path, then runs its block for each request. This one answers GET /posts with status 200 and every row of Note:

blog_api.orb
route GET "/posts" {
  val posts = Note.all()
  return ok 200 posts
}

Path patterns

Segments starting with : or wrapped in {...} capture one non-empty segment; * matches one segment without capturing. A static route always wins over a parameter pattern covering the same path. Captured values are percent-decoded by req.param, and a trailing slash is tolerated. A trailing :name... segment captures the rest of the path, slashes included: route GET "/files/:path..." makes req.param("path") answer a/b/c for /files/a/b/c. The ... segment must end the path, and an empty remainder never matches. Dispatch order is static, then single-segment, then multi-segment:

patterns.orb
route GET "/notes/:id" {
  val id = req.param("id")
  return ok { note: id }
}

What each request binds for the route above:

Request req.param("id")
GET /notes/abc%201 abc 1

Prefer :name. {name} means the same thing and both spellings stay supported.

Reading the request

The req surface has one accessor per input channel. In this route, req.query("author") reads the ?author= value and the route branches on it:

blog_api.orb
route GET "/posts/search" {
  val author = req.query("author")
  if (author == "") {
      return ok 200 Note.all()
  }
  val found = Note.where("author_id = ?", author)
  return ok 200 found
}
Accessor Reads Notes
req.query(name) ?name= value Missing value is "" — branch on it, as above
req.param(name) :name / {name} / decoded segment Percent-decoded for you
req.body() Raw request body req.json() is the same body
req.header(name) Request header
req.bearer_token() Authorization: Bearer token Missing token is ""
req.role() / req.has_role(name) Caller role Checked against the sessions table
req.file(field, dir) Multipart upload Saves the file to disk under dir

The route above produces (canned, from blog_api.orb):

Call Status Body
curl "http://127.0.0.1:8080/posts/search" 200 [{"id":"note_101",...}] (seeded rows)
curl "http://127.0.0.1:8080/posts/search?author=usr_admin" 200 [{"id":"note_103","author_id":"usr_admin",...}]

Bearer and role checks follow the examples/sqlite_notes.orb matrix: reads need a token for a row in sessions, and some writes additionally require the admin role.

Answering, ok vs err

return ok sends a success and err sends a failure. The compiler checks the split, so crossing it is a type error:

You write Result
a 2xx/3xx status through ok accepted
a 4xx/5xx status through err accepted
a 4xx/5xx status through ok rejected at check time
err with a 2xx/3xx status rejected at check time

Each case below pairs the route source with a curl call and the status and body that call produces. Pick a case to see it:

GET /posts/search
route GET "/posts/search" {
    val author = req.query("author")
    if (author == "") {
        return ok 200 Note.all()
    }
    val found = Note.where("author_id = ?", author)
    return ok 200 found
}
Response
$ curl "http://127.0.0.1:8080/posts/search"
[{"id":"note_101",...}]  (seeded rows)

Status 200

Canned response, verified against examples/blog_api.orb on orbit 0.1.0 (feature/0.1.0-stability at 31591e66)

Inside a route, res.header(name, value) and res.cookie(name, value) stage response headers (the cookie is sent with Path=/), so a redirect is an ordinary ok with a Location. A name containing a colon, or a name or value containing CR or LF, is refused:

redirect.orb
route GET "/old" {
  res.header("Location", "/new")
  return ok 302 "moved"
}

Bodies, objects and raw strings

Objects are the canonical success body: every value inside is escaped on the way out, which a hand-written JSON string does not do for you. A raw triple-quoted string stays the form for error bodies and for payloads that are mostly static text. In this file, /health returns an object and /private answers 401:

bodies.orb
route GET "/health" {
  return ok { status: "UP", uptime_seconds: system.uptime() }
}

route GET "/private" {
  err 401 "unauthorized"
}

ok always sends application/json. For an HTML page, bind orbit_response_html and return it; the body is sent as-is, so escape any user input first:

page.orb
extern fn orbit_response_html(status: int, body: string) -> Response

route GET "/" {
  return orbit_response_html(200, "<h1>hi</h1>")
}

When a body mixes interpolation with escaping, std/json offers jsonQuote — a runtime string escaper for the hand-built case. Object literals already escape on serialization; jsonQuote is for the interpolation case, which does not.

Rate budgets

A route can carry a per-route admission budget after the path, enforced per source IP by the admission gate:

blog_api.orb
// C6 load gate: 20 rps sustained, burst 20.
route GET "/health" limit 20 / s burst 20 {
  return ok 200 """{"status":"UP"}"""
}

// Burst probe: tiny bucket so 25 rapid requests always trip 429s.
route GET "/gate-burst" limit 5 / s burst 5 {
  return ok 200 """{"status":"burst-ok"}"""
}

limit takes a positive integer rate plus an optional /unit window. burst sets the bucket capacity and defaults to the rate when omitted or zero.

Unit Window
min 60000 ms
sec, s 1000 ms
ms 1 ms
none 1000 ms

In the example, /gate-burst has a bucket of 5, so 25 rapid requests always trip 429s. The compiler rejects a rate that is not > 0, a window that is not > 0, or a negative burst. Routes without limit fall back to the global gate only.

What next

The reads above come from models: the SQLite page covers Note.all, Note.where, ? placeholders and what first boot seeds.

Type to search commands, errors, modules and concepts.