Build an API to Count GitHub Profile Views With Rust, Actix, and MongoDB | by Max Koverdyaev | Aug, 2022

Call the API endpoints and create your own badges to display views stats.

1*DA4NuS6wqXrgK fPGH HHA

In this post we will learn how to build a simple API to count profile views on GitHub using Rust, Actix and MongoDB. You may consider it a tutorial of how to start a typical HTTP API project from scratch.

The idea of collecting view counts is simple. We need to embed a tracking image into a github page which users can view and display the collected stats. To display the collected stats we will use the shields project which allows to create badges with custom data providers.

First of all you need to set up proper tools to start developing. Here’s the minimal list:

  • Rust Language — the Rust SDK to build and run applications
  • VSCode — an editor to modify your application source code
  • Rust Analyzer — a VSCode extension that provides a better experience for development with Rust
  • Docker Desktop — to build or use Docker images

Docker is also useful for developing your applications locally in case you need a database instance without installing one in your operating system.

Let’s create a Rust project with this cargo command:

cargo new counter

Now you can open the project folder in VSCode and add dependencies that are needed for developing an HTTP API. In this project the dependencies will be:

  • Actix — a popular Rust web framework
  • MongoDB — the Rust driver for MongoDB
  • Serde — a Rust library for JSON serialization/deserialization

You can do it either editing Cargo.toml file or by this command in the project directory:

cargo add actix-web mongodb serde

You also need to modify the serde feature flags to allow using of the derive macro for more declarative serialization.

So the dependencies section will look like this:

actix-web = "4.1.0"
mongodb = "2.3.0"
serde = { version = "1.0.140", features = ["derive"] }

Before we start to code we need a MongoDB instance to store out application data. We will make it by using the official Docker image from DockerHub Registry.

Let’s create a Docker container for the local development:

docker run -d --name local-mongo \
-p 27017:27017 \

Once it’s done you can connect to your local MongoDB with this connection string mongodb://admin:pass@localhost:27017.

To allow the application to access the database you have to add the connection string to the config.toml file in the .cargo directory of the project:

DATABASE_URL = "mongodb://admin:pass@localhost:27017"

You may also need this command to (re)start the MongoDB container:

docker restart local-mongo

In this project we will have two layers to organize the code. The first layer will represent a data service that we will use to work with a MongoDB collection to add and count GitHub profile views. The second layer will expose API endpoints for tracking views and retrieving collected stats.

Let’s make the data service with those two functions described above:

And the endpoint layer for our API:

Let’s wrap it up together:

To run a web server just build and start the application:

cargo run

So now you can call the API endpoints and create your own badges to display views stats.


  • Example how to collect and display the views of a GitHub profile.
  • Complete source code can be found here.

Hope you find this tutorial helpful.

News Credit

%d bloggers like this: