# Turso + Rocket

> Set up Turso in your Rocket project in minutes
> Canonical HTML: https://tursodb.cn/docs/sdk/rust/guides/rocket/
> Upstream Source (MIT): https://docs.turso.tech/sdk/rust/guides/rocket
> Language: en

---
![Rocket banner](/images/turso-docs/guides/rocket-banner.png)

## Prerequisites

Before you start, make sure you:

* [Install the Turso CLI](/docs/cli/installation/)
* [Sign up or login to Turso](/docs/cli/authentication/#signup)
* Have a Rocket app — [learn more](https://rocket.rs/v0.5/guide/getting-started/)

**1. Retrieve database credentials**

You will need an existing database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

You will want to store these as environment variables.

</div></div>


**2. Add the libsql crate to the project**

```bash
cargo add libsql
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Optionally, you can add a package such as [`dotenvy`](https://docs.rs/dotenvy/latest/dotenvy) to help you work with `.env` files:

```bash
cargo add dotenvy
```

</div></div>


**3. Execute SQL**

```rust
#[get("/todos")]
async fn get_todos() -> Json<Vec<Todo>> {
    dotenv().expect(".env file not found");

    let url = env::var("TURSO_DATABASE_URL").expect("TURSO_DATABASE_URL not found!");
    let token = env::var("TURSO_AUTH_TOKEN").expect("TURSO_AUTH_TOKEN not found!");

    let db = Database::open_remote(url, token).unwrap();
    let conn = db.connect().unwrap();

    let mut response = conn.query("select * from todos", ()).await.unwrap();

    let mut todos: Vec<Todo> = Vec::new();
    while let Some(row) = response.next().unwrap() {
        let todo: Todo = Todo {
            task: row.get(0).unwrap(),
        };
        todos.push(todo);
    }

    Json(todos)
}

#[launch]
fn rocket() -> _ {
    dotenv().ok();
    rocket::build().mount("/", routes![get_todos])
}
```

## Examples

<div class="doc-cards">
<a class="doc-card" href="https://github.com/tursodatabase/examples/tree/master/app-todo-rocket" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>Turso + Rocket Todo List</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">See the full source code</p></a>
</div>
