Services & Routes
Tezz abstracts away HTTP server boilerplate, allowing you to focus purely on business logic.
The Service Block
A service block declares an HTTP server and binds it to a specific port.
service UserAPI on 8080 {
// Routes go here
}
Routing
Inside a service, you define endpoints using the route block. You specify the HTTP method (GET, POST, PUT, DELETE) and the path.
route GET "/users" {
// ...
}
route POST "/users" {
// ...
}
Dynamic Path Parameters
You can capture variables from the URL path using a colon (:). The captured values are available in the implicitly provided params object.
route GET "/users/:id" {
let userId = params.id
print("Fetching user: " + userId)
}
The Request Object
Every route automatically injects a request object containing the incoming HTTP data.
request.method- The HTTP method (e.g., "GET").request.url- The request URL path.request.query- An object containing parsed query parameters.request.json()- Parses and returns the JSON body payload.
Sending Responses
Use the respond keyword to instantly send a JSON response to the client. You provide an HTTP status code followed by an object.
route POST "/login" {
let body = request.json()
if body.username == "admin" && body.password == "123" {
respond 200 { success: true, token: "abc-123" }
} else {
respond 401 { success: false, error: "Invalid credentials" }
}
}