David's Blog

Building an API Server in Rust

By David Li on Saturday, 24 November 2023 13:00:00 GMT

Rust is a systems programming language that is known for its performance, safety, and concurrency. It’s a great choice for building an API server, as it can handle a large number of requests efficiently and securely.

In this post, we’ll explore how to build a simple API server using Rust and the popular web framework, Rocket.

First, let’s set up our project. We’ll need to install Rust and its package manager, Cargo, if we haven’t already. We can do this by following the instructions on the Rust website.

Once Rust is installed, we can create a new project by running the following command:

cargo new api-server

This will create a new directory called api-server with the basic structure of a Rust project.

Next, we’ll need to add Rocket as a dependency. We can do this by adding the following to our Cargo.toml file:

rocket = "0.5.0-rc.1"

Now we’re ready to start building our API server. Let’s start by defining a simple route that returns a JSON response. We can do this by adding the following to our src/main.rs file:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Message {
    content: String,

fn index() -> Json<Message> {
    Json(Message {
        content: "Hello, world!".to_string(),

fn rocket() -> _ {
    rocket::build().mount("/", routes![index])

In this code, we define a Message struct that represents the data we want to return in our JSON response. We derive the Serialize and Deserialize traits from the serde crate to enable serialization and deserialization of our struct.

We then define a get route for the root path (/) that returns a Json response containing an instance of our Message struct.

Finally, we define a rocket function that launches our Rocket application and mounts our route.

We can now run our API server by running the following command:

cargo run

This will start our server and make it available at http://localhost:8000. If we visit this URL in our web browser, we should see a JSON response with the content {“content”:“Hello, world!”}.

That’s the basics of building an API server using Rust and Rocket! Of course, there’s a lot more we can do, such as adding more routes, handling different HTTP methods, and connecting to a database. But this should give you a good starting point for your own API server projects.

To add a database

First, you’ll need to add the diesel and dotenv crates to your Cargo.toml file:

diesel = { version = "1.4.7", features = ["postgres"] }
dotenv = "0.15.0"

Next, you’ll need to create a .env file in the root of your project and add your database URL:


Make sure to replace user, password, localhost, and database_name with your own values.

Now you can use the diesel crate to establish a connection to your database. Here’s an example of how to do this:

extern crate diesel;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

fn establish_connection() -> PgConnection {

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
        .expect(&format!("Error connecting to {}", database_url))

fn main() {
    let connection = establish_connection();

In this code, we use the dotenv crate to load the values from our .env file. We then use the env::var function to get the value of the DATABASE_URL environment variable.

We use the PgConnection::establish method to establish a connection to our PostgreSQL database using the provided database URL. If the connection is successful, we return the connection object.

That’s it! You can now use the connection object to interact with your database using the diesel crate.

I hope this helps! Let me know if you have any questions or if there’s anything else I can help with.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-05-06.