Metadata-Version: 2.4
Name: js-api
Version: 0.4.0
Summary: Custom API written in Python using FastAPI
Author-email: Jack Stockley <jack@jstockley.com>
Project-URL: Homepage, https://github.com/jnstockley/api
Project-URL: Repository, https://github.com/jnstockley/api.git
Project-URL: Issues, https://github.com/jnstockley/api/issues
Keywords: starter,template,python
Classifier: Programming Language :: Python :: 3
Requires-Python: <3.16,>=3.15
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi[standard]==0.165.0
Requires-Dist: sqlalchemy==2.1.9
Requires-Dist: sqlmodel==0.0.50
Requires-Dist: python-dotenv==1.2.4
Requires-Dist: psycopg[binary]==3.3.9
Requires-Dist: nctalk==0.0.4
Requires-Dist: jinja2==3.1.6
Dynamic: license-file

# API
This is a custom API, written in Python using FastAPI, to help me accomplish tasks the can be improved through a RESTful API

## Setup
Simplest setup is to start from [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) and [template.env](https://github.com/jnstockley/api/blob/dev/template.env), which should be renamed to `.env`

### Environment Vairables
- `API_KEY` - A long, random string used to authenticate API requests. The health check is public, and the GitHub webhook uses its own signing secret.
- `DATABASE_URL` - The URL to connect to postgres DB. Must start with `postgresql+psycopg://`. Should be in the format specifiec in [template.env](https://github.com/jnstockley/api/blob/dev/template.env)
- `TZ` - Timezone of the container
- `PGTZ` - Timezone the Postgres container should use
- `NEXTCLOUD_URL` - Base URL (must be `https://`) of the Nextcloud instance used by the Talk webhooks
- `NEXTCLOUD_TALK_TOKEN` - Conversation token of the registered [Nextcloud Talk bot](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) used by `/webhook/databasus`
- `NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Nextcloud Talk bot used by `/webhook/databasus`
- `BESZEL_NEXTCLOUD_TALK_TOKEN` - Conversation token for the separate Beszel Talk bot used by `/webhook/beszel`
- `BESZEL_NEXTCLOUD_TALK_SECRET` - Shared secret of the registered Beszel Talk bot
- `GITHUB_WEBHOOK_SECRET` - Signing secret configured on the GitHub webhook
- `GITHUB_NEXTCLOUD_TALK_TOKEN` - Conversation token for GitHub code scanning notifications
- `GITHUB_NEXTCLOUD_TALK_SECRET` - Shared secret of the Talk bot that sends GitHub notifications

## Webhooks
- `POST /webhook/databasus` - Forwards a `header` and `message` to a Nextcloud Talk conversation via the [Nextcloud Talk Bot API](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/), using the [nctalk](https://pypi.org/project/nctalk/) package. Requires the `X-API-KEY` header and a JSON body: `{"header": "...", "message": "..."}`
- `POST /webhook/beszel` - Forwards Beszel's generic JSON notifications (`{"title": "...", "message": "..."}`) using a separate Talk bot. Requires the `X-API-KEY` header. Posts the title in bold followed by the message. Returns `{"status": "sent"}` on success, `422` for invalid payloads, `500` for missing or insecure Talk configuration, or `502` when delivery fails.
- `POST /webhook/github/code-scanning` - Verifies GitHub's `X-Hub-Signature-256` signature and sends a Talk message with the repository, alert number, and alert link. It handles `created`, `fixed`, `closed_by_user`, `reopened`, `reopened_by_user`, and `updated_assignment`. Signed pings, unrelated events, and other actions such as `appeared_in_branch` return `{"status": "ignored"}`.

  For Trivy findings, the message also includes Package, Severity, Installed Version, and Fixed Version when GitHub includes those details in the finding message.

### Beszel setup

1. Register a separate bot named **Beszel** using Nextcloud's [bot management commands](https://nextcloud-talk.readthedocs.io/en/latest/bot-list/) and enable it in the destination Talk conversation.
2. Set `NEXTCLOUD_URL`, `BESZEL_NEXTCLOUD_TALK_TOKEN` (the destination conversation token), and `BESZEL_NEXTCLOUD_TALK_SECRET` (the new bot's shared secret) in `.env`, then recreate the API container to load the settings. Beszel credentials are required; this endpoint does not fall back to the Databasus bot.
3. In Beszel, open **Settings > Notifications** and add this [generic webhook URL](https://beszel.dev/guide/notifications/generic), replacing the host and API key:

   ```text
   generic://api.example.com/webhook/beszel?template=json&@X-API-KEY=YOUR_URL_ENCODED_API_KEY
   ```

   The `generic://` service uses HTTPS by default. URL-encode the API key if it contains special characters. `template=json` supplies the `title` and `message` fields, and `@X-API-KEY` adds the authentication header.
4. Send a test notification from Beszel, then enable alerts in its systems table.

### GitHub setup

1. Enable a Nextcloud Talk bot in the destination conversation. Set `NEXTCLOUD_URL`, `GITHUB_NEXTCLOUD_TALK_TOKEN`, `GITHUB_NEXTCLOUD_TALK_SECRET`, and a random `GITHUB_WEBHOOK_SECRET` in `.env`, then recreate the API container.
2. In the GitHub repository or organization, create a webhook with the HTTPS payload URL `https://api.example.com/webhook/github/code-scanning` and content type `application/json`. Use the value of `GITHUB_WEBHOOK_SECRET` in GitHub's **Secret** field and select **Code scanning alerts** under individual events. [GitHub webhook setup](https://docs.github.com/en/webhooks/using-webhooks/creating-webhooks)

## How to Access
Using the [compose.yml](https://github.com/jnstockley/api/blob/dev/compose.yml) file, you can access the API at `http://<IP>:5000/health-check`. If everything is setup correctly, you should see `{"status":"ok"}`
