%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/rezakur1/api.rezakurnia.com/zh-router/
Upload File :
Create Path :
Current File : //home2/rezakur1/api.rezakurnia.com/zh-router/README.md

# KurniaDev AI Gateway — Node.js + Fastify

## Requirements
- Node.js 20+
- MySQL 8+
- Nginx for production

## Install
```bash
cp .env.example .env
# Edit .env with DB and provider credentials
npm install
mysql -u root -p < database/schema.sql
npm run create-key
npm start
```
Generated API key is shown once. Insert its generated SQL into MySQL and keep the plaintext key private.

## API examples
```bash
curl https://ai.kurniadev.com/v1/models -H "Authorization: Bearer kd_live_YOUR_KEY"
```
```bash
curl https://ai.kurniadev.com/v1/chat/completions \
 -H "Authorization: Bearer kd_live_YOUR_KEY" -H "Content-Type: application/json" \
 -d '{"model":"MODEL_NAME","messages":[{"role":"user","content":"Hello"}]}'
```
Streaming:
```bash
curl -N https://ai.kurniadev.com/v1/chat/completions \
 -H "Authorization: Bearer kd_live_YOUR_KEY" -H "Content-Type: application/json" \
 -d '{"model":"MODEL_NAME","stream":true,"messages":[{"role":"user","content":"Hello"}]}'
```

### Auth & self-service API keys
```bash
# Register akun
curl -X POST http://127.0.0.1:3000/auth/register \
  -H "Content-Type: application/json" \
  -d '{"name":"Budi","email":"budi@example.com","password":"rahasia123"}'

# Login -> dapat session token (opaque, berlaku 24 jam)
curl -X POST http://127.0.0.1:3000/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"budi@example.com","password":"rahasia123"}'

# Buat API key sendiri (kredit awal 0; plaintext hanya tampil sekali)
curl -X POST http://127.0.0.1:3000/me/keys \
  -H "Authorization: Bearer SESSION_TOKEN" -H "Content-Type: application/json" \
  -d '{"name":"App Mobile"}'

# Daftar & revoke key milik sendiri
curl http://127.0.0.1:3000/me/keys -H "Authorization: Bearer SESSION_TOKEN"
curl -X DELETE http://127.0.0.1:3000/me/keys/123 -H "Authorization: Bearer SESSION_TOKEN"

# Logout (invalidasi session)
curl -X POST http://127.0.0.1:3000/auth/logout -H "Authorization: Bearer SESSION_TOKEN"
```
Endpoint auth berada di `/auth/*` dan `/me/*` — di luar `/v1/*` yang di-proxy ke provider.

## Included
- `/v1/*` reverse proxy with query-string forwarding
- Provider key kept server-side; client key checked by SHA-256 hash
- Direct response piping, including SSE streaming
- Basic usage metadata logging
- `/health` endpoint
- Register/login/logout (`/auth/*`) dengan scrypt + session token opaque (hash di DB)
- Self-service API keys (`/me/*`): buat, daftar, revoke — hanya milik sendiri

## Production caveats
This is a starter, not a complete paid API platform. Credit balance is only checked for a positive value; **no credit is debited**. Before charging customers, implement model-specific pricing, provider usage parsing (including streaming usage), atomic debit/reservation, refunds and reconciliation. Add rate/concurrency limits, request validation, monitoring and abuse protection. Do not log prompts or credentials. Review provider-specific headers and endpoint compatibility. Configure TLS, secure `.env`, DB permissions and Nginx buffering off.

Zerion Mini Shell 1.0