EN VI

Tài liệu API

REST API để tích hợp với TOSE.

Giới thiệu

TOSE API là RESTful API cho phép quản lý workspaces, projects, deployments, databases và nhiều hơn nữa.

Base URL

https://api-v2.tose.sh

Định dạng Response

// Thành công
{ "success": true, "data": { ... } }

// Lỗi
{ "success": false, "error": "Thông báo lỗi" }

Xác thực

Tất cả requests cần API key qua header X-API-Key.

curl https://api-v2.tose.sh/api/workspaces \
  -H "X-API-Key: tose_sk_your_key_here"

Tạo API Key

POST /api/workspaces/:slug/api-keys
{ "name": "My Key", "expires_in_days": 90 }

// Response (key chỉ hiển thị MỘT lần!)
{
  "id": "...",
  "key": "tose_sk_abcdef...",
  "key_prefix": "tose_sk_abcd..."
}

Xử lý lỗi

StatusÝ nghĩa
400Bad Request - Tham số không hợp lệ
401Unauthorized - API key sai hoặc thiếu
402Payment Required - Số dư ví không đủ
403Forbidden - Không có quyền
404Not Found - Không tìm thấy
409Conflict - Đã tồn tại
500Internal Server Error

Workspaces

POST/api/workspaces

Tạo workspace mới.

{ "name": "My Workspace", "slug": "my-workspace" }
GET/api/workspaces

Liệt kê tất cả workspaces.

GET/api/workspaces/:slug
PUT/api/workspaces/:slug
DELETE/api/workspaces/:slug

Thành viên

GET/api/workspaces/:slug/members
POST/api/workspaces/:slug/members
{ "user_id": "...", "role": "admin" }  // Roles: owner, admin, member
PUT/api/workspaces/:slug/members/:user_id
DELETE/api/workspaces/:slug/members/:user_id

API Keys

POST/api/workspaces/:slug/api-keys
GET/api/workspaces/:slug/api-keys
DELETE/api/workspaces/:slug/api-keys/:key_id

Projects

POST/api/workspaces/:slug/projects
{
  "name": "My App",
  "port": 3000,
  "replicas": 1,
  "cpu": "0.5",
  "ram": "0.5"
}
GET/api/workspaces/:slug/projects
GET/api/workspaces/:slug/projects/:project_slug
PUT/api/workspaces/:slug/projects/:project_slug
DELETE/api/workspaces/:slug/projects/:project_slug

Trạng thái & Điều khiển

GET/api/workspaces/:slug/projects/:project_slug/status
// Response
{
  "project": { "slug": "my-app", "status": "running" },
  "pods": { "ready": 1, "total": 1, "items": [...] },
  "resources": {
    "cpu": { "used": "50m", "limit": "500m" },
    "memory": { "used": "128Mi", "limit": "512Mi" }
  }
}
POST/api/workspaces/:slug/projects/:project_slug/restart

Git Connection

POST/api/workspaces/:slug/projects/:project_slug/git
{
  "repo_full_name": "owner/repo",
  "branch": "main",
  "installation_id": 12345,
  "auto_deploy": true
}
PUT/api/workspaces/:slug/projects/:project_slug/git
DELETE/api/workspaces/:slug/projects/:project_slug/git

Biến môi trường

GET/api/workspaces/:slug/projects/:project_slug/env
POST/api/workspaces/:slug/projects/:project_slug/env
{ "key": "DATABASE_URL", "value": "postgres://...", "is_secret": true }
PUT/api/workspaces/:slug/projects/:project_slug/env/:env_id
DELETE/api/workspaces/:slug/projects/:project_slug/env/:env_id

Cập nhật hàng loạt

PUT/api/workspaces/:slug/projects/:project_slug/env/bulk
{
  "variables": { "DB_HOST": "localhost", "API_KEY": "secret" },
  "merge": true
}
// Response: { "created": 2, "updated": 1, "deleted": 0 }

Deployments

GET/api/workspaces/:slug/projects/:project_slug/deployments

Query params: page, limit (1-50)

GET/api/workspaces/:slug/projects/:project_slug/deployments/:id
POST/api/workspaces/:slug/projects/:project_slug/deploy

Tạo deployment mới (build từ Git).

POST/api/workspaces/:slug/projects/:project_slug/redeploy

Deploy lại với image hiện tại (không build lại).

POST/api/workspaces/:slug/projects/:project_slug/deployments/:id/stop
POST/api/workspaces/:slug/projects/:project_slug/deployments/:id/rollback

Tên miền

GET/api/workspaces/:slug/projects/:project_slug/domains
POST/api/workspaces/:slug/projects/:project_slug/domains
{ "domain": "app.example.com" }
DELETE/api/workspaces/:slug/projects/:project_slug/domains/:domain_id

Databases

POST/api/workspaces/:slug/databases
{ "name": "my-postgres", "type": "postgresql" }
// Types: postgresql, mysql, redis, mongo
GET/api/workspaces/:slug/databases
GET/api/workspaces/:slug/databases/:db_slug
DELETE/api/workspaces/:slug/databases/:db_slug

Templates

GET/api/workspaces/available-templates

Danh sách templates có sẵn.

// Response
[{
  "name": "Goclaw",
  "key": "goclaw",
  "description": "AI-powered development platform",
  "default_port": "3000",
  "default_cpu": "2",
  "default_ram": "4",
  "required_envs": [
    { "key": "GOCLAW_POSTGRES_DSN", "placeholder": "postgresql://..." }
  ]
}]
POST/api/workspaces/:slug/templates
{ "name": "My App", "template_name": "goclaw" }
GET/api/workspaces/:slug/templates
GET/api/workspaces/:slug/templates/:template_slug
PUT/api/workspaces/:slug/templates/:template_slug
DELETE/api/workspaces/:slug/templates/:template_slug
POST/api/workspaces/:slug/templates/:template_slug/deploy
{ "mode": "image" }  // hoặc "build"
POST/api/workspaces/:slug/templates/:template_slug/stop

Template Env Vars

GET/api/workspaces/:slug/templates/:template_slug/envs
POST/api/workspaces/:slug/templates/:template_slug/envs
[
  { "key": "DATABASE_URL", "value": "postgres://..." },
  { "key": "API_KEY", "value": "secret", "is_secret": true }
]
DELETE/api/workspaces/:slug/templates/:template_slug/envs/:key

Template Domains

GET/api/workspaces/:slug/templates/:template_slug/domains
POST/api/workspaces/:slug/templates/:template_slug/domains
{ "domain": "app.example.com" }
DELETE/api/workspaces/:slug/templates/:template_slug/domains/:domain_id

Template Deployments

GET/api/workspaces/:slug/templates/:template_slug/deployments
GET/api/workspaces/:slug/templates/:template_slug/deployments/:id

Ví & Thanh toán

GET/api/workspaces/:slug/wallet

Xem số dư ví.

{ "balance": 50000, "currency": "VND" }
GET/api/workspaces/:slug/wallet/transactions

Lịch sử giao dịch.

GET/api/workspaces/:slug/wallet/usage

Thống kê sử dụng.

POST/api/workspaces/:slug/wallet/topup/sepay
{ "amount": 100000 }
POST/api/workspaces/:slug/wallet/topup/polar
{ "amount": 10 }
PUT/api/workspaces/:slug/wallet/alert-threshold
{ "threshold": 10000 }

Logs

GET/api/workspaces/:slug/projects/:project_slug/logs

Query params: limit, direction (asc/desc), start, end

GET/api/workspaces/:slug/projects/:project_slug/logs/download

Tải logs dạng file gzip.

GET/api/workspaces/:slug/projects/:project_slug/logs/usage

Thống kê dung lượng log.

POST/api/workspaces/:slug/projects/:project_slug/logs/analyze

Phân tích logs bằng AI.

{ "logs": "nội dung log lỗi..." }

Uptime Monitoring

GET/api/workspaces/:slug/projects/:project_slug/uptime
// Response
{
  "enabled": true,
  "check_interval_sec": 60,
  "check_url": "https://my-app.tose.sh/health",
  "discord_webhook_url": "...",
  "notify_email": "admin@example.com",
  "status": "up",
  "last_checked_at": "...",
  "last_response_ms": 120
}
PUT/api/workspaces/:slug/projects/:project_slug/uptime
{
  "enabled": true,
  "check_interval_sec": 60,
  "discord_webhook_url": "https://discord.com/api/webhooks/...",
  "notify_email": "admin@example.com"
}

WebSocket

Stream logs real-time qua WebSocket. Truyền API key qua header X-API-Key.

Project Build Logs

WS/api/workspaces/:slug/projects/:project_slug/deployments/:id/logs

Project Live Logs

WS/api/workspaces/:slug/projects/:project_slug/live-logs

Template Build Logs

WS/api/workspaces/:slug/templates/:template_slug/deployments/:id/logs

Template Live Logs

WS/api/workspaces/:slug/templates/:template_slug/live-logs

Ví dụ kết nối

// Sử dụng WebSocket với headers (thư viện ws)
const WebSocket = require('ws');
const ws = new WebSocket(
  'wss://api-v2.tose.sh/api/workspaces/my-ws/projects/my-app/live-logs',
  { headers: { 'X-API-Key': 'tose_sk_...' } }
);

ws.onmessage = (event) => {
  if (event.data === '[END]') { ws.close(); return; }
  if (event.data.startsWith('[ERROR]')) { console.error(event.data); return; }
  console.log(event.data);
};

Lưu ý: Browser WebSocket không hỗ trợ custom headers. Sử dụng thư viện ws npm hoặc truyền API key qua query parameter ?api_key=tose_sk_... cho browser clients.

Tin nhắn đặc biệt

Tin nhắnÝ nghĩa
[END]Stream kết thúc (build hoàn tất)
[ERROR] ...Có lỗi xảy ra