%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/ |
| Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/pages/ApiDocsPage.tsx |
import { useState, type ReactNode } from "react";
import { Link } from "react-router-dom";
import Badge from "@/components/ui/Badge";
import CodeBlock from "@/components/docs/CodeBlock";
import DocsToc from "@/components/docs/DocsToc";
import EndpointHeader from "@/components/docs/EndpointHeader";
import { useSEO } from "@/hooks/useSEO";
import {
API_BASE_URL,
AUTH_TABS,
CHAT_BODY,
CHAT_PARAMS,
CHAT_REQUEST_TABS,
CHAT_RESPONSE,
DOC_TOC,
ERROR_ROWS,
MODELS_RESPONSE,
MODELS_TABS,
SDK_TABS,
STREAM_RESPONSE,
STREAM_TABS,
} from "@/data/chatCompletionDocs";
function Section({
id,
title,
intro,
children,
}: {
id: string;
title: string;
intro?: string;
children: ReactNode;
}) {
return (
<section id={id} className="scroll-mt-24">
<h3 className="text-base font-semibold text-white sm:text-lg">{title}</h3>
{intro && (
<p className="mt-1.5 max-w-3xl text-sm leading-relaxed text-slate-500">
{intro}
</p>
)}
<div className="mt-4 space-y-4">{children}</div>
</section>
);
}
function CopyButton({ text }: { text: string }) {
const [copied, setCopied] = useState(false);
const copy = async () => {
try {
await navigator.clipboard.writeText(text);
} catch {
/* clipboard tidak tersedia — user bisa menyalin manual */
}
setCopied(true);
window.setTimeout(() => setCopied(false), 2000);
};
return (
<button
type="button"
onClick={copy}
className={`shrink-0 rounded-md border px-2.5 py-1 text-[11px] font-medium transition-colors ${
copied
? "border-emerald-400/25 bg-emerald-500/10 text-emerald-300"
: "border-white/[0.08] bg-white/[0.03] text-slate-400 hover:bg-white/[0.06] hover:text-white"
}`}
>
{copied ? "Tersalin" : "Salin"}
</button>
);
}
function statusTone(status: string) {
if (status.startsWith("2")) {
return "border-emerald-400/20 bg-emerald-500/10 text-emerald-300";
}
if (status.startsWith("4")) {
return "border-amber-400/20 bg-amber-500/10 text-amber-300";
}
return "border-red-400/20 bg-red-500/10 text-red-300";
}
const QUICK_STEPS = [
{
num: "1",
title: "Buat API key",
desc: "Buat key baru di halaman API Keys. Key lengkap hanya ditampilkan satu kali.",
link: "/keys",
linkLabel: "Buka API Keys",
},
{
num: "2",
title: "Simpan sebagai environment variable",
desc: "Simpan key di server Anda, jangan di kode frontend atau repository publik.",
code: "KURNIADEV_API_KEY=kd_live_...",
},
{
num: "3",
title: "Kirim request pertama",
desc: "Panggil endpoint chat completions dengan model yang tersedia di katalog.",
code: "POST /v1/chat/completions",
},
];
export default function ApiDocsPage() {
useSEO({
title: "API Documentation",
description:
"Panduan integrasi Chat Completions KURNIADEV AI: autentikasi, parameter, streaming, dan penanganan error.",
canonicalPath: "/docs",
});
const baseUrl = API_BASE_URL;
return (
<div className="space-y-6">
{/* Header halaman */}
<div className="flex flex-col gap-4 border-b border-white/[0.06] pb-6 sm:flex-row sm:items-end sm:justify-between">
<div>
<div className="mb-2 flex items-center gap-2">
<Badge tone="info">v1</Badge>
<span className="text-[11px] font-medium text-slate-500">
OpenAI-compatible API
</span>
</div>
<h2 className="text-xl font-bold tracking-tight text-white sm:text-2xl">
API Documentation
</h2>
<p className="mt-1 max-w-2xl text-sm leading-relaxed text-slate-500">
Panduan lengkap memakai Chat Completions di KURNIADEV AI Gateway —
mulai dari membuat API key sampai streaming respons.
</p>
</div>
<Link
to="/keys"
className="inline-flex shrink-0 items-center gap-2 rounded-xl border border-brand-400/25 bg-brand-500/10 px-3.5 py-2 text-xs font-semibold text-brand-300 transition hover:bg-brand-500/15 hover:text-brand-200"
>
Buat API Key
<svg
className="h-3.5 w-3.5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M13.5 4.5L21 12l-7.5 7.5M21 12H3"
/>
</svg>
</Link>
</div>
{/* Base URL */}
<div className="panel p-4 sm:p-5">
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<p className="text-[10px] font-semibold uppercase tracking-widest text-slate-500">
Base URL
</p>
<p className="mt-1 text-[11px] text-slate-500">
Semua contoh di halaman ini memakai base URL produksi.
</p>
</div>
<span className="chip">Produksi</span>
</div>
<div className="mt-3 flex items-center gap-2 rounded-xl border border-white/[0.06] bg-ink-950/80 px-3 py-2.5">
<span className="text-[10px] font-semibold uppercase tracking-wider text-slate-600">
API
</span>
<code className="min-w-0 flex-1 truncate font-mono text-xs text-brand-300">
{baseUrl}/v1
</code>
<CopyButton text={`${baseUrl}/v1`} />
</div>
</div>
{/* TOC versi mobile */}
<div className="xl:hidden">
<div className="flex gap-1.5 overflow-x-auto pb-1">
{DOC_TOC.map((item) => (
<a
key={item.id}
href={`#${item.id}`}
className="shrink-0 rounded-full border border-white/[0.08] bg-white/[0.03] px-3 py-1.5 text-[11px] font-medium text-slate-400 transition hover:bg-white/[0.06] hover:text-white"
>
{item.title}
</a>
))}
</div>
</div>
<div className="grid gap-10 xl:grid-cols-[minmax(0,1fr)_220px]">
<div className="min-w-0 space-y-10">
{/* Mulai cepat */}
<Section
id="quickstart"
title="Mulai Cepat"
intro="Tiga langkah untuk mengirim request pertama ke gateway."
>
<div className="grid gap-3 sm:grid-cols-3">
{QUICK_STEPS.map((step) => (
<div
key={step.num}
className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4"
>
<span className="flex h-6 w-6 items-center justify-center rounded-full border border-brand-400/25 bg-brand-500/10 text-[11px] font-bold text-brand-300">
{step.num}
</span>
<p className="mt-3 text-sm font-semibold text-white">
{step.title}
</p>
<p className="mt-1 text-xs leading-relaxed text-slate-500">
{step.desc}
</p>
{step.link && (
<Link
to={step.link}
className="mt-3 inline-flex text-[11px] font-semibold text-brand-300 hover:text-brand-200"
>
{step.linkLabel} →
</Link>
)}
{step.code && (
<code className="mt-3 block overflow-x-auto rounded-lg border border-white/[0.06] bg-ink-950/80 px-2.5 py-1.5 font-mono text-[10px] text-slate-400">
{step.code}
</code>
)}
</div>
))}
</div>
</Section>
{/* Autentikasi */}
<Section
id="authentication"
title="Autentikasi"
intro="Semua request ke endpoint /v1/* wajib menyertakan API key pada header Authorization."
>
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<p className="text-xs leading-relaxed text-slate-400">
Format header:
</p>
<code className="mt-2 block overflow-x-auto rounded-lg border border-white/[0.06] bg-ink-950/80 px-3 py-2 font-mono text-xs text-slate-200">
Authorization: Bearer kd_live_...
</code>
<ul className="mt-3 space-y-1.5 text-xs leading-relaxed text-slate-500">
<li>
• API key dibuat dari halaman{" "}
<Link
to="/keys"
className="font-medium text-brand-300 hover:text-brand-200"
>
API Keys
</Link>{" "}
dan hanya ditampilkan satu kali.
</li>
<li>
• Key yang sudah di-revoke atau nonaktif akan ditolak dengan
status <span className="text-amber-300">401</span>.
</li>
<li>
• Jangan menaruh key di kode frontend, repository publik, atau
aplikasi mobile yang bisa dibongkar.
</li>
</ul>
</div>
<CodeBlock tabs={AUTH_TABS} baseUrl={baseUrl} title="Contoh autentikasi" />
</Section>
{/* Chat completions */}
<Section
id="chat-completions"
title="Chat Completions"
intro="Endpoint utama untuk menghasilkan jawaban dari model chat. Format request dan respons kompatibel dengan OpenAI Chat Completions."
>
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<EndpointHeader method="POST" path="/v1/chat/completions" />
<p className="mt-3 text-xs leading-relaxed text-slate-500">
Kirim riwayat percakapan pada field <code className="text-slate-300">messages</code>.
Sertakan pesan <code className="text-slate-300">system</code> di awal
untuk mengatur gaya jawaban, lalu pesan <code className="text-slate-300">user</code>{" "}
untuk pertanyaan.
</p>
</div>
<div>
<p className="mb-2 text-xs font-semibold uppercase tracking-wider text-slate-500">
Parameter
</p>
<div className="overflow-x-auto rounded-xl border border-white/[0.06]">
<table className="w-full min-w-[640px] text-left text-sm">
<thead>
<tr className="border-b border-white/[0.06] bg-white/[0.02] text-[10px] uppercase tracking-wider text-slate-500">
<th className="px-4 py-2.5 font-semibold">Field</th>
<th className="px-4 py-2.5 font-semibold">Tipe</th>
<th className="px-4 py-2.5 font-semibold">Keterangan</th>
</tr>
</thead>
<tbody className="divide-y divide-white/[0.04]">
{CHAT_PARAMS.map((param) => (
<tr key={param.name}>
<td className="whitespace-nowrap px-4 py-3 align-top">
<code className="font-mono text-xs text-white">
{param.name}
</code>
{param.required && (
<span className="ml-2 text-[9px] font-bold uppercase tracking-wide text-red-300">
wajib
</span>
)}
</td>
<td className="whitespace-nowrap px-4 py-3 align-top font-mono text-[11px] text-slate-500">
{param.type}
</td>
<td className="px-4 py-3 align-top text-xs leading-relaxed text-slate-400">
{param.description}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
<div>
<p className="mb-2 text-xs font-semibold uppercase tracking-wider text-slate-500">
Contoh body
</p>
<CodeBlock
tabs={[{ label: "JSON", language: "json", code: CHAT_BODY }]}
baseUrl={baseUrl}
title="Contoh body chat completions"
/>
</div>
<div>
<p className="mb-2 text-xs font-semibold uppercase tracking-wider text-slate-500">
Contoh request
</p>
<CodeBlock
tabs={CHAT_REQUEST_TABS}
baseUrl={baseUrl}
title="Contoh request chat completions"
/>
</div>
<div>
<p className="mb-2 text-xs font-semibold uppercase tracking-wider text-slate-500">
Contoh respons
</p>
<CodeBlock
tabs={[{ label: "JSON", language: "json", code: CHAT_RESPONSE }]}
baseUrl={baseUrl}
title="Contoh respons chat completions"
/>
<p className="mt-2 text-[11px] leading-relaxed text-slate-500">
Jawaban ada di <code className="text-slate-300">choices[0].message.content</code>.
Field <code className="text-slate-300">usage</code> menampilkan jumlah
token yang dipakai untuk perhitungan biaya.
</p>
</div>
</Section>
{/* Streaming */}
<Section
id="streaming"
title="Streaming"
intro="Aktifkan stream: true agar jawaban muncul bertahap. Respons dikirim sebagai Server-Sent Events (SSE) dengan awalan data: dan diakhiri data: [DONE]."
>
<CodeBlock
tabs={STREAM_TABS}
baseUrl={baseUrl}
title="Contoh streaming"
/>
<div>
<p className="mb-2 text-xs font-semibold uppercase tracking-wider text-slate-500">
Bentuk respons SSE
</p>
<CodeBlock
tabs={[
{ label: "SSE", language: "text", code: STREAM_RESPONSE },
]}
baseUrl={baseUrl}
title="Contoh respons streaming"
/>
</div>
</Section>
{/* Models */}
<Section
id="models"
title="Daftar Model"
intro="Gunakan endpoint ini untuk melihat model yang tersedia. Nilai id dari respons dipakai sebagai field model pada chat completions."
>
<div className="rounded-xl border border-white/[0.06] bg-white/[0.02] p-4">
<EndpointHeader method="GET" path="/v1/models" />
</div>
<CodeBlock tabs={MODELS_TABS} baseUrl={baseUrl} title="Contoh daftar model" />
<CodeBlock
tabs={[{ label: "JSON", language: "json", code: MODELS_RESPONSE }]}
baseUrl={baseUrl}
title="Contoh respons daftar model"
/>
<p className="text-[11px] leading-relaxed text-slate-500">
Katalog, harga per 1 juta token, dan tes model bisa dilihat di
halaman{" "}
<Link
to="/models"
className="font-medium text-brand-300 hover:text-brand-200"
>
Model
</Link>
. Nama model harus persis sama dengan yang ada di katalog.
</p>
</Section>
{/* Error & kuota */}
<Section
id="errors"
title="Error & Kuota"
intro="Semua error memakai bentuk yang sama: { error: { message, type } }. Berikut status yang paling sering muncul."
>
<div className="overflow-x-auto rounded-xl border border-white/[0.06]">
<table className="w-full min-w-[560px] text-left text-sm">
<thead>
<tr className="border-b border-white/[0.06] bg-white/[0.02] text-[10px] uppercase tracking-wider text-slate-500">
<th className="px-4 py-2.5 font-semibold">Status</th>
<th className="px-4 py-2.5 font-semibold">Arti</th>
<th className="px-4 py-2.5 font-semibold">Keterangan</th>
</tr>
</thead>
<tbody className="divide-y divide-white/[0.04]">
{ERROR_ROWS.map((row) => (
<tr key={row.status}>
<td className="whitespace-nowrap px-4 py-3 align-top">
<span
className={`inline-flex rounded-md border px-2 py-0.5 font-mono text-[11px] font-bold ${statusTone(
row.status,
)}`}
>
{row.status}
</span>
</td>
<td className="whitespace-nowrap px-4 py-3 align-top text-xs font-medium text-slate-300">
{row.title}
</td>
<td className="px-4 py-3 align-top text-xs leading-relaxed text-slate-400">
{row.description}
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="rounded-xl border border-amber-400/20 bg-amber-500/[0.06] p-4">
<p className="text-xs font-semibold text-amber-200">
Tentang status 402
</p>
<p className="mt-1.5 text-xs leading-relaxed text-amber-100/70">
Gateway memakai dua sumber kuota: saldo wallet dan paket aktif.
Jika tidak ada paket aktif dan saldo wallet habis, request
ditolak dengan 402. Jika paket aktif tetapi kuota request atau
token sudah terpakai, request juga ditolak dengan 402. Isi saldo
di halaman{" "}
<Link
to="/topup"
className="font-semibold text-amber-200 underline-offset-2 hover:underline"
>
Top Up
</Link>{" "}
atau beli paket di halaman{" "}
<Link
to="/packages"
className="font-semibold text-amber-200 underline-offset-2 hover:underline"
>
Paket
</Link>
.
</p>
</div>
<CodeBlock
tabs={[
{
label: "JSON",
language: "json",
code: `{
"error": {
"message": "Kuota layanan sedang tidak tersedia. Silakan coba lagi nanti atau hubungi admin.",
"type": "insufficient_quota"
}
}`,
},
]}
baseUrl={baseUrl}
title="Contoh respons 402"
/>
</Section>
{/* Tips integrasi */}
<Section
id="integration"
title="Tips Integrasi"
intro="Gateway ini kompatibel dengan OpenAI SDK. Cukup ganti base URL dan API key."
>
<CodeBlock tabs={SDK_TABS} baseUrl={baseUrl} title="Contoh OpenAI SDK" />
<ul className="space-y-2 text-xs leading-relaxed text-slate-500">
<li>
• Simpan API key di environment variable server, bukan di kode
yang dikirim ke browser.
</li>
<li>
• Gunakan timeout yang wajar (misalnya 30–120 detik) dan retry
dengan exponential backoff untuk status 429, 500, 502, dan 503.
</li>
<li>
• Untuk jawaban panjang, aktifkan streaming agar pengguna
melihat respons lebih cepat.
</li>
<li>
• Pantau pemakaian dan biaya di halaman{" "}
<Link
to="/usage"
className="font-medium text-brand-300 hover:text-brand-200"
>
Usage
</Link>{" "}
dan{" "}
<Link
to="/billing"
className="font-medium text-brand-300 hover:text-brand-200"
>
Billing
</Link>
.
</li>
</ul>
</Section>
</div>
<DocsToc items={DOC_TOC} />
</div>
</div>
);
}