Files
nyeki-api/src/handlers/mod.rs
T
2025-07-13 18:46:34 +00:00

116 lines
4.6 KiB
Rust

use std::sync::Arc;
use axum::{Router, routing::get};
use dedent::dedent;
use crate::state::State;
pub mod v1;
pub fn router() -> Router<Arc<State>> {
Router::new()
.route("/", get(docs))
.nest("/v1", v1::router())
}
async fn docs() -> &'static str {
dedent!(
"
Welcome to Nyeki's API! This is a tiny API I built to provide some basic information I
found myself needing in more than one place. This at the moment only includes Discord and
GitHub profiles, but I may add more stuff as I see myself needing it.
The API is versioned, so API endpoints will always be prefixed with `/v{version}`. The
current version is 1 (i.e. stuff is prefixed by `/v1`). All responses are JSON (or
redirects).
The API is not meant to be used by anyone but me, so I don't guarantee that it will always
be available or that it will always return the same data (nor that it's in any way reliable
in production). You're free to use it if you want, but don't expect any serious API
guarantees.
The current endpoints are:
GET /v1/profile
Return's the user's profile (username, name, avatar, banner, and URL).
Query parameters:
- `id`: The ID of the user you want to get the profile of (or their username
for supported sources).
- `source`: The source of the profile (either `discord` or `github`).
Response:
```ts
{
// The user's full username.
\"username\": String,
// The user's name to display.
\"name\": String,
// The URL to the biggest version of the user's avatar. WEBP versions are
// preferred when available.
\"avatar_url\": String,
// The URL to the user's banner, if available.
\"banner_url\": String | null,
// The URL to the user's profile on the source.
\"url\": String,
}
```
GET /v1/profile/avatar
Redirects to the user's avatar URL.
Query parameters:
- `id`: The ID of the user you want to get the profile of (or their username
for supported sources).
- `source`: The source of the profile (either `discord` or `github`).
GET /v1/profile/banner
Redirects to the user's banner URL, or returns nothing if the user has no banner.
Query parameters:
- `id`: The ID of the user you want to get the profile of (or their username
for supported sources).
- `source`: The source of the profile (either `discord` or `github`).
GET /v1/profile/url
Redirects to the user's profile URL on the source.
Query parameters:
- `id`: The ID of the user you want to get the profile of (or their username
for supported sources).
- `source`: The source of the profile (either `discord` or `github`).
They all take 2 query parameters: `id` and `source`. The `id` is the ID of the user you
want to get the profile of, and the `source` is the source of the profile. The source can
be either `discord` or `github`.
For GitHub IDs, numeric IDs will be considered the user's ID, while anything else will be
considered a username. For Discord, they're all snowflakes since there's no API to fetch
users by username. As a sidenote, if you were to want to fetch a Discord user by username,
I'd try to use a user token to send a friend request to the user, then read the ID from the
pending friend requests, fetch the profile, and remove the friend request. Feel free to
play with that (Discord won't like you automating with your user token, it's against ToS).
Errors are quite simple and they all follow the same structure:
```ts
{
// The HTTP status code of the error.
\"status\": Number,
// A human-readable message describing the error.
\"message\": String,
}
```
This API is open source and available at https://git.nyeki.dev/nyeki/api to self-host.
Liked it? Found it useful? A donation would be appreciated! You can do so at
https://ko-fi.com/nyeki.
"
)
}