Skip to main content

Rust Quickstart

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

  1. Create a Cargo project

    Create a new Cargo project.

    cargo new my_app
    cd my_app
  2. Install the Convex client and server libraries

    To get started, install the convex npm package which enables you to write your backend.

    And also install the convex Rust client library, the tokio runtime, and dotenv for working with .env files.

    npm install convex && cargo add convex tokio dotenv
  3. Setup a Convex dev deployment

    Next, run npx convex dev. This will prompt you to log in with GitHub, create a project, and save your production and deployment URLs.

    It will also create a convex/ folder for you to write your backend API functions in. The dev command will then continue running to sync your functions with your dev deployment in the cloud.

    npx convex dev
  4. Create sample data for your database

    In a new terminal window, create a sampleData.jsonl file with some sample data.

    sampleData.jsonl
    {"text": "Buy groceries", "isCompleted": true}
    {"text": "Go for a swim", "isCompleted": true}
    {"text": "Integrate Convex", "isCompleted": false}
  5. Add the sample data to your database

    Now that your project is ready, add a tasks table with the sample data into your Convex database with the import command.

    npx convex import tasks sampleData.jsonl
  6. Expose a database query

    Add a new file getTasks.js in the convex/ folder with a query function that loads the data.

    The default export declares an API function named after the file, "getTasks".

    convex/getTasks.js
    import { query } from "./_generated/server";

    export default query(async ({ db }) => {
    return await db.query("tasks").collect();
    });
  7. Connect the app to your backend

    In the file src/main.rs, create a ConvexClient and use it to fetch from your "getTasks" API.

    src/main.rs
    use std::{
    collections::BTreeMap,
    env,
    };

    use convex::ConvexClient;

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

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

    let mut client = ConvexClient::new(&deployment_url).await.unwrap();
    let result = client.query("getTasks", BTreeMap::new()).await.unwrap();
    println!("{result:#?}");
    }
  8. Run the app

    Run the app and see the serialized list of tasks.

    cargo run