This example shows how to make a simple TODO app using Rocket and a shared Shuttle Postgres DB.

The following routes are provided:

  • GET /todos/<id> - Get a to-do item by ID.
  • POST /todos - Create a to-do item. Takes “note” as a JSON body parameter.

You can clone the example below by running the following (you’ll need cargo-shuttle installed):

cargo shuttle init --from shuttle-hq/shuttle-examples \
 --subfolder rocket/postgres


extern crate rocket;

use rocket::response::status::BadRequest;
use rocket::serde::json::Json;
use rocket::State;
use serde::{Deserialize, Serialize};
use shuttle_runtime::CustomError;
use sqlx::{Executor, FromRow, PgPool};

async fn retrieve(id: i32, state: &State<MyState>) -> Result<Json<Todo>, BadRequest<String>> {
    let todo = sqlx::query_as("SELECT * FROM todos WHERE id = $1")
        .map_err(|e| BadRequest(e.to_string()))?;


#[post("/", data = "<data>")]
async fn add(
    data: Json<TodoNew>,
    state: &State<MyState>,
) -> Result<Json<Todo>, BadRequest<String>> {
    let todo = sqlx::query_as("INSERT INTO todos(note) VALUES ($1) RETURNING id, note")
        .map_err(|e| BadRequest(e.to_string()))?;


struct MyState {
    pool: PgPool,

async fn rocket(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_rocket::ShuttleRocket {

    let state = MyState { pool };
    let rocket = rocket::build()
        .mount("/todos", routes![retrieve, add])


struct TodoNew {
    pub note: String,

#[derive(Serialize, FromRow)]
struct Todo {
    pub id: i32,
    pub note: String,


Once you’ve cloned the example, try launching it locally using cargo shuttle run. Once you’ve verified that it runs successfully, try using cURL in a new terminal to send a POST request:

curl -X POST -d '{"note":"Hello world!"}' -H 'Content-Type: application/json' \

Assuming the request was successful, you’ll get back a JSON response with the ID and Note of the record you just created. If you try the following cURL command, you should be able to then retrieve the message you stored:

curl http://localhost:8000/todos/<id>

Interested in extending this example? Here’s as couple of ideas:

  • Add update and delete routes
  • Add static files to show your records

If you want to explore other frameworks, we have more examples with popular ones like Tower and Warp. You can find them right here.
Be sure to check out the examples repo for many more examples!