Back to Templates

Secure your Rust Axum API

A Rust API service using Unkey for API key validation with the Axum framwork.

Written by
unrenamed
Framework
Axum
Language
Rust
A Rust API service using Unkey for API key validation with the Axum framwork.

Protect your Rust + Axum API with Unkey

This example shows how to secure a Rust + Axum API using Unkey for API key management. You'll learn how to protect public and private routes with minimal setup and start authenticating users quickly.

Quickstart

Create a root key

  1. Go to /settings/root-keys and click on the "Create New Root Key" button.
  2. Enter a name for the key.
  3. Select the following workspace permissions: create_key, read_key, encrypt_key and decrypt_key.
  4. Click "Create".

Create your API

  1. Go to https://app.unkey.com/apis and click on the "Create New API" button.
  2. Give it a name.
  3. Click "Create".

Create your first API key

  1. Click "Create Key" in the top right corner.
  2. Feel the form with any key information you want or leave it empty.
  3. Click "Create"
  4. Copy the key and save it somewhere safe.

Set up the example

  1. Clone the repository to your local machine:
1git clone git@github.com:unrenamed/unkey-rust-axum
2cd unkey-rust-axum
  1. Create a .env file in the root directory and populate it with the following environment variables:
1PORT=3000
2UNKEY_ROOT_KEY=your-unkey-root-key
3UNKEY_API_ID=your-unkey-api-id

Ensure you replace your-unkey-root-key and your-unkey-api-id with your actual Unkey credentials.

  1. Start the server:
1cargo run

The server will start and listen on the port specified in the .env file (default is 3000).

  1. Test the public route as a guest:
1 curl http://localhost:3000/public
  1. Test the public route as an authorized user by passing the API key in the header:
1 curl http://localhost:3000/public -H "Authorization: Bearer <YOUR_API_KEY>"
  1. Test the protected route, which requires valid authorization:
1 curl http://localhost:3000/protected -H "Authorization: Bearer <YOUR_API_KEY>"

Protect your API.
Start today.

2500 verifications and 100K successful rate‑limited requests per month. No CC required.