import { Billing } from "@opencode/cloud-core/billing.js" import { Key } from "@opencode/cloud-core/key.js" import { action, createAsync, query, useAction, useSubmission, json } from "@solidjs/router" import { createSignal, For, onMount, Show } from "solid-js" import { getActor } from "~/context/auth" import { withActor } from "~/context/auth.withActor" import { IconCopy, IconCheck } from "~/component/icon" import "./[id].css" import { User } from "@opencode/cloud-core/user.js" import { Actor } from "@opencode/cloud-core/actor.js" ///////////////////////////////////// // Keys related queries and actions ///////////////////////////////////// const listKeys = query(async () => { "use server" return withActor(() => Key.list()) }, "key.list") const createKey = action(async (name: string) => { "use server" return json( withActor(() => Key.create({ name })), { revalidate: listKeys.key }, ) }, "key.create") const removeKey = action(async (id: string) => { "use server" return json( withActor(() => Key.remove({ id })), { revalidate: listKeys.key }, ) }, "key.remove") ///////////////////////////////////// // Billing related queries and actions ///////////////////////////////////// const getBillingInfo = query(async () => { "use server" return withActor(async () => { const actor = Actor.assert("user") const [user, billing, payments, usage] = await Promise.all([ User.fromID(actor.properties.userID), Billing.get(), Billing.payments(), Billing.usages(), ]) return { user, billing, payments, usage } }) }, "billingInfo") const createCheckoutUrl = action(async (successUrl: string, cancelUrl: string) => { "use server" return withActor(() => Billing.generateCheckoutUrl({ successUrl, cancelUrl })) }, "checkoutUrl") const createPortalUrl = action(async (returnUrl: string) => { "use server" return withActor(() => Billing.generatePortalUrl({ returnUrl })) }, "portalUrl") export default function () { ///////////////// // Keys section ///////////////// const keys = createAsync(() => listKeys(), { deferStream: true, }) const createKeyAction = useAction(createKey) const removeKeyAction = useAction(removeKey) const createKeySubmission = useSubmission(createKey) const [showCreateForm, setShowCreateForm] = createSignal(false) const [keyName, setKeyName] = createSignal("") const [copiedKeyId, setCopiedKeyId] = createSignal(null) const formatDate = (date: Date) => { return date.toLocaleDateString() } const formatDateForTable = (date: Date) => { const options: Intl.DateTimeFormatOptions = { day: "numeric", month: "short", hour: "numeric", minute: "2-digit", hour12: true, } return date.toLocaleDateString("en-GB", options).replace(",", ",") } const formatDateUTC = (date: Date) => { const options: Intl.DateTimeFormatOptions = { weekday: "short", year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "2-digit", second: "2-digit", timeZoneName: "short", timeZone: "UTC", } return date.toLocaleDateString("en-US", options) } const formatKey = (key: string) => { if (key.length <= 11) return key return `${key.slice(0, 7)}...${key.slice(-4)}` } const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text) } catch (error) { console.error("Failed to copy to clipboard:", error) } } const copyKeyToClipboard = async (text: string, keyId: string) => { try { await navigator.clipboard.writeText(text) setCopiedKeyId(keyId) setTimeout(() => setCopiedKeyId(null), 1500) } catch (error) { console.error("Failed to copy to clipboard:", error) } } const handleCreateKey = async () => { if (!keyName().trim()) return try { await createKeyAction(keyName().trim()) setKeyName("") setShowCreateForm(false) } catch (error) { console.error("Failed to create API key:", error) } } const handleDeleteKey = async (keyId: string) => { if (!confirm("Are you sure you want to delete this API key?")) { return } try { await removeKeyAction(keyId) } catch (error) { console.error("Failed to delete API key:", error) } } ///////////////// // Billing section ///////////////// const billingInfo = createAsync(() => getBillingInfo(), { deferStream: true, }) const createCheckoutUrlAction = useAction(createCheckoutUrl) const createCheckoutUrlSubmission = useSubmission(createCheckoutUrl) const handleBuyCredits = async () => { try { const baseUrl = window.location.href const checkoutUrl = await createCheckoutUrlAction(baseUrl, baseUrl) if (checkoutUrl) { window.location.href = checkoutUrl } } catch (error) { console.error("Failed to get checkout URL:", error) } } return (
{/* Title */}

Gateway

Coding models optimized for use with opencode. Learn more.

{/* API Keys Section */}

API Keys

Manage your API keys for accessing opencode services.

setKeyName(e.currentTarget.value)} onKeyPress={(e) => e.key === "Enter" && handleCreateKey()} />
} >

Create an opencode Gateway API key

} > {(key) => ( )}
Name Key Created
{key.name}
copyKeyToClipboard(key.key, key.id)} title="Click to copy API key"> {formatKey(key.key)} } >
{formatDateForTable(key.timeCreated)}
{/* Balance Section */}

Balance

Add credits to your account.

{ const balanceStr = ((billingInfo()?.billing?.balance ?? 0) / 100000000).toFixed(2) return balanceStr === "0.00" || balanceStr === "-0.00" })(), }} > $ {(() => { const balanceStr = ((billingInfo()?.billing?.balance ?? 0) / 100000000).toFixed(2) return balanceStr === "-0.00" ? "0.00" : balanceStr })()}
{/* Usage Section */}

Usage History

Recent API usage and costs.

0} fallback={

Make your first API call to get started.

} > {(usage) => { const totalTokens = usage.inputTokens + usage.outputTokens + (usage.reasoningTokens || 0) const date = new Date(usage.timeCreated) return ( ) }}
Date Model Tokens Cost
{formatDateForTable(date)} {usage.model} {totalTokens.toLocaleString()} ${((usage.cost ?? 0) / 100000000).toFixed(4)}
{/* Payments Section */} 0}>

Payments History

Recent payment transactions.

{(payment) => { const date = new Date(payment.timeCreated) return ( ) }}
Date Payment ID Amount
{formatDateForTable(date)} {payment.id} ${((payment.amount ?? 0) / 100000000).toFixed(2)}
) }