import { t } from "@lingui/core/macro" import { Trans, useLingui } from "@lingui/react/macro" import { KeyRoundIcon, LoaderCircleIcon, ShieldXIcon } from "lucide-react" import { useState } from "react" import { TokenValue } from "@/components/token-value" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { Button, buttonVariants } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Separator } from "@/components/ui/separator" import { toast } from "@/components/ui/use-toast" import { pb } from "@/lib/api" import { cn, formatTimeUnit, getHubURL } from "@/lib/utils" const DAY = 24 * 60 * 60 const NEVER = "never" function showError(error: unknown) { toast({ title: t`Error`, description: (error as Error).message, variant: "destructive", }) } export default function ApiKeysSettings() { return (

API Keys

API keys let scripts and integrations use the Beszel API as your user. A key has the same access as your account and works until it expires or is revoked.

) } function SectionCreate() { const { t } = useLingui() const [duration, setDuration] = useState(String(30 * DAY)) const [isLoading, setIsLoading] = useState(false) const [created, setCreated] = useState<{ token: string; expires: string } | null>(null) const durations = [ { value: DAY, label: formatTimeUnit(1, "day") }, { value: 7 * DAY, label: formatTimeUnit(7, "day") }, { value: 30 * DAY, label: formatTimeUnit(30, "day") }, { value: 90 * DAY, label: formatTimeUnit(90, "day") }, { value: 365 * DAY, label: formatTimeUnit(1, "year") }, { value: NEVER, label: t`Never` }, ] async function createKey() { setIsLoading(true) try { const data = await pb.send<{ token: string; expires: string }>("/api/beszel/api-keys", { method: "POST", body: duration === NEVER ? { noExpiry: true } : { duration: Number(duration) }, }) setCreated(data) } catch (error) { showError(error) } finally { setIsLoading(false) } } const expires = created?.expires ? new Date(created.expires).toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }) : "" return (

Create API key

{created && (

Copy this key now. It won't be shown again.

{expires && (

Expires {expires}

)}

Send the key in the Authorization header of each request:

curl -H "Authorization: $BESZEL_API_KEY" {getHubURL()}/api/collections/systems/records
)}
) } function SectionRevoke() { const [open, setOpen] = useState(false) const [isLoading, setIsLoading] = useState(false) async function revokeKeys() { setIsLoading(true) try { const { token } = await pb.send<{ token: string }>("/api/beszel/api-keys/revoke", { method: "POST" }) // the old session token is now invalid, so keep this tab logged in with the new one pb.authStore.save(token, pb.authStore.record) // reload so realtime subscriptions reconnect with the new token window.location.reload() } catch (error) { showError(error) setIsLoading(false) } } return (

Revoke API keys

Revoking invalidates all of your API keys and signs you out of other devices. Changing your password has the same effect.

Revoke all API keys? Every API key you have created will stop working immediately, and you will be signed out of other devices. Cancel Revoke
) }