# Sinatra

A Sinatra 4 JSON API starter in Ruby 3.2, written in modular style and served by Puma through Rack. It is for Ruby developers who want a minimal API skeleton with JSON error responses and Minitest already set up.

- Framework: Sinatra (Ruby)
- Category: Starters / REST APIs
- Creates: a server app
- Plan: Free
- Price: Free
- Author: Light Cloud
- Page: https://templates.light-cloud.com/starters/rest-apis/sinatra-api-starter
- Updated: 2026-11-21

## Features

- Sinatra 4.2 on Puma 6 and Rack 3, started by rackup from config.ru
- Modular style: Application < Sinatra::Base, with routes as extensions under app/routes/
- GET / returns {"message": "Hello from Sinatra on Light Cloud"} through a small json helper
- Unknown routes return 404 as {"error": "Not Found"} and errors return {"error": "Something went wrong"}
- show_exceptions is off, so an error never sends a stack trace to the client
- Minitest and Rack::Test tests for the root route and the JSON 404, run with bundle exec rake
- Gem versions locked in Gemfile.lock; the deploy skips the development and test gems

## What you need

- Free plan: it runs as a server app, and the Free plan includes 3 server apps
- To run it locally: Ruby 3.2 or newer and Bundler
- No API keys: it starts with nothing to configure

## About

Settings, the json helper and the not_found and error handlers sit in app/application.rb, and each file in app/routes/ is a module with self.registered(app) that the app pulls in with register. That keeps the class small as you add resources, and it mounts in Rack or tests without global state. There is only a root route, so the first job is a new module such as Routes::Things, and maybe a /health route for monitoring. Light Cloud runs it with bundle exec rackup on PORT and APP_ENV=production, redeploying on each push to your copy.

## Questions

### How do I add a route to a modular Sinatra app?

Create a module in app/routes/ shaped like root.rb, with a self.registered(app) method that calls app.get, app.post and so on. Then require_relative it in app/application.rb and add register Routes::YourModule.

### What is the difference between classic and modular Sinatra?

Classic style defines routes at the top level of a script. Modular style, used here, puts the app in a Sinatra::Base subclass that Rack, tests and other apps can mount without global state.

### How do I return JSON errors in Sinatra?

Define not_found and error blocks that return JSON, as app/application.rb does, and turn off show_exceptions so Sinatra does not send its HTML error page instead.

### Does this Sinatra starter reload when I change a file?

No. Restart bundle exec rackup -p 3000 after a change, or add a reloader gem such as rerun to the development group.

### Can I deploy this Sinatra API without Light Cloud?

Yes. It is a plain Rack app: any host with Ruby 3.2 runs it with bundle exec rackup -p $PORT -o 0.0.0.0 and APP_ENV=production, and any server that reads config.ru works too.
