Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions .github/skills/usage/use-rest-api/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
---
name: use-rest-api
description: Use the Torrust Tracker REST API. Covers authentication, all endpoints (stats, metrics, torrents, auth keys, whitelist), and making announce/scrape requests to verify API behaviour. Triggers on "use API", "test API", "call REST API", "query API", "API endpoint", "curl tracker", "tracker client", "announce request", or "verify API".
metadata:
author: torrust
version: "1.0"
---

# Use REST API

## Prerequisites

A running tracker with the REST API enabled. The default development config starts the API on port 1212:

```bash
cargo run
```

## Skill Links

This skill depends on these artifacts. If any of them change, review this skill.

- `share/default/config/tracker.development.sqlite3.toml`
- `packages/axum-rest-api-server/src/v1/middlewares/auth.rs`
- `packages/axum-rest-api-server/src/routes.rs`
- `packages/axum-rest-api-server/src/v1/routes.rs`

Use the marker `skill-link: use-rest-api` in affected artifacts.

## Authentication

All API endpoints (except `/api/health_check`) require an access token.

### Header Method (preferred)

```bash
curl -H "Authorization: Bearer MyAccessToken" http://localhost:1212/api/v1/stats
```

### Query Parameter Method

```bash
curl "http://localhost:1212/api/v1/stats?token=MyAccessToken"
```

### Configuration

Tokens are defined in the TOML config file under `[http_api.access_tokens]`:

```toml
[http_api.access_tokens]
admin = "MyAccessToken"
```

Every token in the map has identical permissions — the label (`admin`) is just a human-readable name.

## Endpoints

All endpoints use `http://localhost:1212` as base (default dev config).

### Health Check

| Method | Endpoint | Auth |
| ------ | ------------------- | ----- |
| GET | `/api/health_check` | ❌ No |

```bash
curl -s http://localhost:1212/api/health_check
```

### Stats

| Method | Endpoint | Auth |
| ------ | ----------------- | ------ |
| GET | `/api/v1/stats` | ✅ Yes |
| GET | `/api/v1/metrics` | ✅ Yes |

```bash
curl -s http://localhost:1212/api/v1/stats -H "Authorization: Bearer MyAccessToken"
curl -s http://localhost:1212/api/v1/metrics -H "Authorization: Bearer MyAccessToken"
```

### Auth Keys

| Method | Endpoint | Auth |
| ------ | ------------------------------------ | ------ |
| POST | `/api/v1/key/{seconds_valid_or_key}` | ✅ Yes |
| DELETE | `/api/v1/key/{seconds_valid_or_key}` | ✅ Yes |
| GET | `/api/v1/keys/reload` | ✅ Yes |
| POST | `/api/v1/keys` | ✅ Yes |

### Whitelist

| Method | Endpoint | Auth |
| ------ | ------------------------------- | ------ |
| POST | `/api/v1/whitelist/{info_hash}` | ✅ Yes |
| DELETE | `/api/v1/whitelist/{info_hash}` | ✅ Yes |
| GET | `/api/v1/whitelist/reload` | ✅ Yes |

### Torrents

| Method | Endpoint | Auth |
| ------ | ----------------------------- | ------ |
| GET | `/api/v1/torrent/{info_hash}` | ✅ Yes |
| GET | `/api/v1/torrents` | ✅ Yes |

## Making Announce Requests with the Tracker Client

The `tracker_client` binary can make BitTorrent announce requests to verify the tracker is working.

### UDP Announce

```bash
cargo run -p torrust-tracker-client --bin tracker_client -- udp announce udp://localhost:6969/announce 0123456789abcdef0123456789abcdef01234567
```

### HTTP Announce

```bash
cargo run -p torrust-tracker-client --bin tracker_client -- http announce http://localhost:7070/announce 0123456789abcdef0123456789abcdef01234567
```

### Scrape

```bash
cargo run -p torrust-tracker-client --bin tracker_client -- udp scrape udp://localhost:6969/announce 0123456789abcdef0123456789abcdef01234567
```

Output defaults to JSON. Use `--format text` for human-readable output.

## Verification Workflow

After making an announce request, verify the API reflects the activity:

1. Check stats changed:

```bash
curl -s http://localhost:1212/api/v1/stats -H "Authorization: Bearer MyAccessToken"
```

Expect `torrents` and `seeders` to increase.

2. Check metrics changed:

```bash
curl -s http://localhost:1212/api/v1/metrics -H "Authorization: Bearer MyAccessToken"
```

Expect protocol-specific counters to increase.

3. Check tracker console logs show the request was received:

```text
active_peers_total=1 active_torrents_total=1
```
1 change: 0 additions & 1 deletion .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ jobs:
cargo publish -p torrust-tracker-axum-rest-api-server
cargo publish -p torrust-tracker-axum-server
cargo publish -p torrust-tracker-rest-api-client
cargo publish -p torrust-tracker-rest-api-core
cargo publish -p torrust-server-lib
cargo publish -p torrust-tracker
cargo publish -p torrust-tracker-client
Expand Down
42 changes: 21 additions & 21 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,27 @@ native IPv4/IPv6 support, private/whitelisted mode, and a management REST API.

All packages live under `packages/`. The workspace version is `3.0.0-develop`.

| Package | Crate Name | Prefix / Layer | Description |
| --------------------------------- | ------------------------------------------------- | -------------- | --------------------------------------------- |
| `axum-health-check-api-server` | `torrust-tracker-axum-health-check-api-server` | `axum-*` | Health monitoring endpoint |
| `axum-http-server` | `torrust-tracker-axum-http-server` | `axum-*` | BitTorrent HTTP tracker server (BEP 3/23) |
| `axum-rest-api-server` | `torrust-tracker-axum-rest-api-server` | `axum-*` | Management REST API server |
| `axum-server` | `torrust-tracker-axum-server` | `axum-*` | Base Axum HTTP server infrastructure |
| `configuration` | `torrust-tracker-configuration` | domain | Config file parsing, environment variables |
| `events` | `torrust-tracker-events` | domain | Domain event definitions |
| `http-protocol` | `torrust-tracker-http-protocol` | `*-protocol` | HTTP tracker protocol (BEP 3/23) parsing |
| `http-core` | `torrust-tracker-http-core` | `*-core` | HTTP-specific tracker domain logic |
| `primitives` | `torrust-tracker-primitives` | domain | Core domain types (InfoHash, PeerId, ...) |
| `rest-api-client` | `torrust-tracker-rest-api-client` | client tools | REST API client library |
| `rest-api-core` | `torrust-tracker-rest-api-core` | client tools | REST API core logic |
| `swarm-coordination-registry` | `torrust-tracker-swarm-coordination-registry` | domain | Torrent/peer coordination registry |
| `test-helpers` | `torrust-tracker-test-helpers` | utilities | Mock servers, test data generation |
| `torrent-repository-benchmarking` | `torrust-tracker-torrent-repository-benchmarking` | benchmarking | Torrent storage benchmarks |
| `tracker-client` | `torrust-tracker-client` | client tools | CLI tracker interaction/testing client |
| `tracker-core` | `torrust-tracker-core` | `*-core` | Central tracker peer-management logic |
| `udp-protocol` | `torrust-tracker-udp-protocol` | `*-protocol` | UDP tracker protocol (BEP 15) framing/parsing |
| `udp-core` | `torrust-tracker-udp-core` | `*-core` | UDP-specific tracker domain logic |
| `udp-server` | `torrust-tracker-udp-server` | server | UDP tracker server implementation |
| Package | Crate Name | Prefix / Layer | Description |
| --------------------------------- | ------------------------------------------------- | --------------- | --------------------------------------------- |
| `axum-health-check-api-server` | `torrust-tracker-axum-health-check-api-server` | `axum-*` | Health monitoring endpoint |
| `axum-http-server` | `torrust-tracker-axum-http-server` | `axum-*` | BitTorrent HTTP tracker server (BEP 3/23) |
| `axum-rest-api-server` | `torrust-tracker-axum-rest-api-server` | `axum-*` | Management REST API server |
| `axum-server` | `torrust-tracker-axum-server` | `axum-*` | Base Axum HTTP server infrastructure |
| `configuration` | `torrust-tracker-configuration` | domain | Config file parsing, environment variables |
| `events` | `torrust-tracker-events` | domain | Domain event definitions |
| `http-protocol` | `torrust-tracker-http-protocol` | `*-protocol` | HTTP tracker protocol (BEP 3/23) parsing |
| `http-core` | `torrust-tracker-http-core` | `*-core` | HTTP-specific tracker domain logic |
| `primitives` | `torrust-tracker-primitives` | domain | Core domain types (InfoHash, PeerId, ...) |
| `rest-api-client` | `torrust-tracker-rest-api-client` | client tools | REST API client library |
| `rest-api-runtime-adapter` | `torrust-tracker-rest-api-runtime-adapter` | runtime adapter | REST API runtime adapter and container wiring |
| `swarm-coordination-registry` | `torrust-tracker-swarm-coordination-registry` | domain | Torrent/peer coordination registry |
| `test-helpers` | `torrust-tracker-test-helpers` | utilities | Mock servers, test data generation |
| `torrent-repository-benchmarking` | `torrust-tracker-torrent-repository-benchmarking` | benchmarking | Torrent storage benchmarks |
| `tracker-client` | `torrust-tracker-client` | client tools | CLI tracker interaction/testing client |
| `tracker-core` | `torrust-tracker-core` | `*-core` | Central tracker peer-management logic |
| `udp-protocol` | `torrust-tracker-udp-protocol` | `*-protocol` | UDP tracker protocol (BEP 15) framing/parsing |
| `udp-core` | `torrust-tracker-udp-core` | `*-core` | UDP-specific tracker domain logic |
| `udp-server` | `torrust-tracker-udp-server` | server | UDP tracker server implementation |

**Extracted packages** — previously part of this workspace, now in their own standalone repositories:

Expand Down
23 changes: 3 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ torrust-tracker-axum-http-server = { version = "3.0.0-develop", path = "packages
torrust-tracker-axum-rest-api-server = { version = "3.0.0-develop", path = "packages/axum-rest-api-server" }
torrust-tracker-axum-server = { version = "3.0.0-develop", path = "packages/axum-server" }
torrust-tracker-rest-api-client = { version = "3.0.0-develop", path = "packages/rest-api-client" }
torrust-tracker-rest-api-core = { version = "3.0.0-develop", path = "packages/rest-api-core" }
torrust-tracker-rest-api-runtime-adapter = { version = "3.0.0-develop", path = "packages/rest-api-runtime-adapter" }
torrust-tracker-rest-api-protocol = { version = "3.0.0-develop", path = "packages/rest-api-protocol" }
torrust-server-lib = "0.1.0"
torrust-clock = "3.0.0"
Expand Down
3 changes: 0 additions & 3 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ COPY packages/http-protocol/Cargo.toml packages/http-protocol/
COPY packages/http-core/Cargo.toml packages/http-core/
COPY packages/primitives/Cargo.toml packages/primitives/
COPY packages/rest-api-client/Cargo.toml packages/rest-api-client/
COPY packages/rest-api-core/Cargo.toml packages/rest-api-core/
COPY packages/rest-api-application/Cargo.toml packages/rest-api-application/
COPY packages/rest-api-protocol/Cargo.toml packages/rest-api-protocol/
COPY packages/rest-api-runtime-adapter/Cargo.toml packages/rest-api-runtime-adapter/
Expand Down Expand Up @@ -127,7 +126,6 @@ RUN mkdir -p \
packages/http-core/benches \
packages/primitives/src \
packages/rest-api-client/src \
packages/rest-api-core/src \
packages/rest-api-application/src \
packages/rest-api-protocol/src \
packages/rest-api-runtime-adapter/src \
Expand Down Expand Up @@ -168,7 +166,6 @@ RUN mkdir -p \
packages/http-core/benches/http_tracker_core_benchmark.rs \
packages/primitives/src/lib.rs \
packages/rest-api-client/src/lib.rs \
packages/rest-api-core/src/lib.rs \
packages/rest-api-application/src/lib.rs \
packages/rest-api-protocol/src/lib.rs \
packages/rest-api-runtime-adapter/src/lib.rs \
Expand Down
5 changes: 1 addition & 4 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ deny = [
"torrust-tracker-axum-rest-api-server",
] },

# udp server — only server-layer + root + rest-api-core (pending fix) + runtime-adapter may depend on it
# udp server — only server-layer + root + runtime-adapter may depend on it
{ crate = "torrust-tracker-udp-server", wrappers = [
"torrust-tracker",
"torrust-tracker-axum-health-check-api-server",
"torrust-tracker-axum-rest-api-server",
"torrust-tracker-rest-api-core",
"torrust-tracker-rest-api-runtime-adapter",
] },

Expand Down Expand Up @@ -84,13 +83,11 @@ deny = [
"torrust-tracker",
"torrust-tracker-axum-http-server",
"torrust-tracker-axum-rest-api-server",
"torrust-tracker-rest-api-core",
"torrust-tracker-rest-api-runtime-adapter",
] },
{ crate = "torrust-tracker-udp-core", wrappers = [
"torrust-tracker",
"torrust-tracker-axum-rest-api-server",
"torrust-tracker-rest-api-core",
"torrust-tracker-rest-api-runtime-adapter",
"torrust-tracker-udp-server",
] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The contexts are ordered by complexity and dependency depth. Follow-up tasks (SI
```

See the `torrent` context for the reference pattern.

- Define port traits in `rest-api-application` for each context's query/command operations.
These are flat files named after the context in `packages/rest-api-application/src/ports/`:

Expand Down Expand Up @@ -140,7 +141,7 @@ The contexts are ordered by complexity and dependency depth. Follow-up tasks (SI
- [#1940](https://github.com/torrust/torrust-tracker/issues/1940) — [SI-2](../1940-1938-si-2-migrate-whitelist-context.md): Migrate `whitelist` context
- [#1941](https://github.com/torrust/torrust-tracker/issues/1941) — [SI-3](../1941-1938-si-3-migrate-auth-key-context.md): Migrate `auth_key` context
- [#1942](https://github.com/torrust/torrust-tracker/issues/1942) — [SI-4](../1942-1938-si-4-migrate-stats-context.md): Migrate `stats` context
- [#1943](https://github.com/torrust/torrust-tracker/issues/1943) — [SI-5](../1943-1938-si-5-deprecate-rest-api-core.md): Deprecate `rest-api-core` and remove from workspace
- [#1943](https://github.com/torrust/torrust-tracker/issues/1943) — [SI-5](../1943-1938-si-5-deprecate-rest-api-core/ISSUE.md): Deprecate `rest-api-core` and remove from workspace
- [#1944](https://github.com/torrust/torrust-tracker/issues/1944) — [SI-6](../1944-1938-si-6-align-rest-api-client.md): Introduce `ApiClient` — a high-level typed client over protocol DTOs

## Contract Evolution Governance
Expand Down
Loading
Loading