> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Quickstart

> Add Bijection to a Rust project

Learn how to query data from Bijection in a Rust app with Tokio.

<Steps>
  <Step title="Install the Bijection CLI">
    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    curl -fsSL https://bijection.com/install.sh | sh
    ```

    See [Install Bijection](/get-started/install) for PATH setup and supported
    platforms. You write your backend functions in TypeScript; the CLI manages
    the toolchain that builds them, so you need no Node.js install.
  </Step>

  <Step title="Create a Cargo project">
    Create a new Cargo project.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    cargo new my_app
    cd my_app
    ```
  </Step>

  <Step title="Add a Bijection backend and the Rust client">
    Run `bijection init` in the project. It adds a `bijection/` folder, where
    you write your backend functions, with a sample schema, query and mutation,
    and `bijection.json`.

    Then add the `bijection` Rust client library, the `tokio` runtime, and
    `dotenvy` for working with `.env` files.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection init && cargo add bijection tokio dotenvy
    ```
  </Step>

  <Step title="Set up a Bijection dev deployment">
    Log this machine in to Bijection, once per machine, then run
    `bijection dev`. The first run creates your dev deployment and saves its URL
    in `.env.local` as `BIJECTION_URL`. The `dev` command then keeps running to
    push your functions in `bijection/` to your dev deployment on every change.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection login
    bijection dev
    ```

    <Note>
      Bijection's hosted service is not yet open for public sign-up, and the CLI
      has no default management service. Before running `bijection login`, set
      `BIJECTION_MANAGEMENT_URL` to the management service URL you were given, in
      your shell's startup file so that every later `bijection` command sees it too:

      ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
      export BIJECTION_MANAGEMENT_URL=https://<your management service>
      ```
    </Note>
  </Step>

  <Step title="Create sample data for your database">
    In a new terminal window, create a `sampleData.jsonl`
    file with some sample data.

    ```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    {"text": "Buy groceries", "isCompleted": true}
    {"text": "Go for a swim", "isCompleted": true}
    {"text": "Integrate Bijection", "isCompleted": false}
    ```
  </Step>

  <Step title="Add the sample data to your database">
    Now that your project is ready, add a `tasks` table
    with the sample data into your Bijection database with
    the `import` command.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    bijection import --table tasks sampleData.jsonl
    ```
  </Step>

  <Step title="Declare the tasks table">
    `bijection init` created `bijection/schema.ts`, which declares the tables of
    your database, starting with a sample `messages` table. Add a `tasks` table
    beside it. The schema gives your functions TypeScript types, and Bijection
    enforces it at runtime.

    ```ts bijection/schema.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    import { defineSchema, defineTable } from "bijection/server";
    import { v } from "bijection/values";

    export default defineSchema({
      messages: defineTable({
        author: v.string(),
        body: v.string(),
      }),
      tasks: defineTable({
        text: v.string(),
        isCompleted: v.boolean(),
      }),
    });
    ```
  </Step>

  <Step title="Expose a database query">
    Add a new file `tasks.ts` in the `bijection/` folder
    with a query function that loads the data.

    Exporting a query function from this file
    declares an API function named after the file
    and the export name, `"tasks:get"`.

    ```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    import { query } from "./_generated/server";

    export const get = query({
      handler: async ({ db }) => {
        return await db.query("tasks").collect();
      },
    });
    ```
  </Step>

  <Step title="Connect the app to your backend">
    In the file `src/main.rs`, create a `BijectionClient` and use it
    to fetch from your `"tasks:get"` API.

    ```rust theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    use std::{
        collections::BTreeMap,
        env,
    };

    use bijection::BijectionClient;

    #[tokio::main]
    async fn main() {
        dotenvy::from_filename(".env.local").ok();
        dotenvy::dotenv().ok();

        let deployment_url = env::var("BIJECTION_URL").unwrap();

        let mut client = BijectionClient::new(&deployment_url).await.unwrap();
        let result = client.query("tasks:get", BTreeMap::new()).await.unwrap();
        println!("{result:#?}");
    }
    ```
  </Step>

  <Step title="Run the app">
    Run the app and see the serialized list of tasks.

    ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
    cargo run
    ```
  </Step>
</Steps>

See the complete [Rust documentation](https://docs.rs/bijection/latest/bijection/).
