n/chit-chatu/Furry · 5 个月前
注册 登录
n/chit-chat · 5 个月前 · 212 次浏览
u/Furry钻石

org.digital OPENAPI发布

Terminal Hosting - BASH API DOCS

基础信息

API 地址: https://nic.org.digital/api
所有请求和响应均为 JSON 格式

认证说明

大部分 API 需要 Bearer Token 认证:

Authorization: Bearer <your_token>

1. 健康检查

curl https://nic.org.digital/api/health

响应:

{"status":"ok","timestamp":"2026-06-05T00:00:00.000Z"}

2. 注册

curl -X POST https://nic.org.digital/api/auth/signup \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"123456"}'

响应:

{
  "token": "eyJhbGciOiJIUzI1NiIs...",
  "refresh_token": "xxx",
  "user": {
    "id": "uuid",
    "email": "[email protected]"
  }
}

3. 登录

curl -X POST https://nic.org.digital/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"123456"}'

响应:

{
  "token": "eyJhbGciOiJIUzI1NiIs...",
  "refresh_token": "xxx",
  "user": {
    "id": "uuid",
    "email": "[email protected]"
  }
}

4. 刷新 Token

curl -X POST https://nic.org.digital/api/auth/refresh \
  -H "Content-Type: application/json" \
  -d '{"refresh_token":"xxx"}'

响应:

{
  "token": "new_token",
  "refresh_token": "new_refresh_token"
}

5. 获取子域名状态

curl https://nic.org.digital/api/subdomain \
  -H "Authorization: Bearer <token>"

响应:

{
  "hasSubdomain": true,
  "subdomain": "myblog",
  "url": "https://myblog.org.digital"
}

6. 注册子域名

curl -X POST https://nic.org.digital/api/subdomain \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"subdomain":"myblog"}'

子域名规则:

  • 仅限小写字母、数字、连字符
  • 长度 3-32 个字符
  • 必须以字母或数字开头和结尾
  • 不能使用保留前缀:nic, api, admin, mail, support, www

响应:

{
  "success": true,
  "subdomain": "myblog",
  "url": "https://myblog.org.digital"
}

错误响应:

{"error":"Subdomain already taken"}
{"error":"Subdomain is reserved"}
{"error":"Invalid subdomain format"}

7. 上传文件

curl -X POST https://nic.org.digital/api/upload \
  -H "Authorization: Bearer <token>" \
  -F "[email protected]" \
  -F "filename=index.html"

文件限制:

  • 仅限 .html / .htm 文件
  • 文件名格式:index.html, index2.html, index3.html...
  • 最大 10MB

响应:

{
  "success": true,
  "filename": "index.html",
  "size": 1234,
  "url": "https://myblog.org.digital/index.html"
}

错误响应:

{"error":"File already exists. Delete it first."}
{"error":"Register a subdomain first with \"domain <name>\""}
{"error":"File too large (max 10MB)"}

8. 获取文件列表

curl https://nic.org.digital/api/files \
  -H "Authorization: Bearer <token>"

响应:

{
  "files": [
    {
      "filename": "index.html",
      "size": 1234,
      "uploaded": "2026-06-05T00:00:00Z"
    },
    {
      "filename": "index2.html",
      "size": 5678,
      "uploaded": "2026-06-05T00:00:00Z"
    }
  ]
}

9. 删除文件

curl -X DELETE https://nic.org.digital/api/files/index.html \
  -H "Authorization: Bearer <token>"

响应:

{"success":true}

10. 获取统计信息

curl https://nic.org.digital/api/stats \
  -H "Authorization: Bearer <token>"

响应:

{
  "email": "[email protected]",
  "user_id": "uuid",
  "has_subdomain": true,
  "subdomain": "myblog",
  "site_url": "https://myblog.org.digital",
  "total_files": 2,
  "total_size_mb": "0.45"
}

状态码说明

状态码 说明
200 成功
400 请求参数错误
401 未认证或 Token 过期
403 无权限(如注册保留子域名)
404 资源不存在
409 冲突(文件已存在/子域名已被占用)
500 服务器错误

在命令行中使用变量的示例

# 保存 token 到变量
TOKEN="your_token_here"

# 使用变量
curl -H "Authorization: Bearer $TOKEN" https://nic.org.digital/api/files

# 获取统计
curl -H "Authorization: Bearer $TOKEN" https://nic.org.digital/api/stats

如果懒得用我的命令行可以自建操作界面(JS)

Terminal Hosting - JavaScript API 文档

基础配置

const API_BASE = 'https://nic.org.digital/api'

// 存储 token
let token = null

1. 健康检查

async function healthCheck() {
  const res = await fetch(`${API_BASE}/health`)
  return res.json()
}

// 使用
healthCheck().then(console.log)
// { status: 'ok', timestamp: '2026-06-05T...' }

2. 注册

async function signup(email, password) {
  const res = await fetch(`${API_BASE}/auth/signup`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email, password })
  })
  return res.json()
}

// 使用
const { token, user } = await signup('[email protected]', '123456')
console.log('Token:', token)
console.log('User:', user)

3. 登录

async function login(email, password) {
  const res = await fetch(`${API_BASE}/auth/login`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email, password })
  })
  const data = await res.json()
  if (data.token) token = data.token
  return data
}

// 使用
const data = await login('[email protected]', '123456')
console.log('登录成功:', data.user)

4. 刷新 Token

async function refreshToken(refreshToken) {
  const res = await fetch(`${API_BASE}/auth/refresh`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ refresh_token: refreshToken })
  })
  const data = await res.json()
  if (data.token) token = data.token
  return data
}

5. 获取子域名状态

async function getSubdomain() {
  const res = await fetch(`${API_BASE}/subdomain`, {
    headers: { 'Authorization': `Bearer ${token}` }
  })
  return res.json()
}

// 使用
const { hasSubdomain, subdomain, url } = await getSubdomain()
console.log(`子域名: ${subdomain}, 网站: ${url}`)

6. 注册子域名

async function registerSubdomain(subdomain) {
  const res = await fetch(`${API_BASE}/subdomain`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({ subdomain })
  })
  return res.json()
}

// 使用
const result = await registerSubdomain('myblog')
if (result.success) {
  console.log(`✅ 注册成功: ${result.url}`)
} else {
  console.log(`❌ 失败: ${result.error}`)
}

7. 上传文件

async function uploadFile(file, filename = 'index.html') {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('filename', filename)
  
  const res = await fetch(`${API_BASE}/upload`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${token}` },
    body: formData
  })
  return res.json()
}

// 使用 - 从 input 选择文件
const fileInput = document.getElementById('fileInput')
const file = fileInput.files[0]
const result = await uploadFile(file, 'index.html')
console.log(`上传成功: ${result.url}`)

// 使用 - 从 URL 获取文件
async function uploadFromUrl(url, filename) {
  const response = await fetch(url)
  const blob = await response.blob()
  const file = new File([blob], filename, { type: 'text/html' })
  return uploadFile(file, filename)
}

8. 获取文件列表

async function listFiles() {
  const res = await fetch(`${API_BASE}/files`, {
    headers: { 'Authorization': `Bearer ${token}` }
  })
  return res.json()
}

// 使用
const { files } = await listFiles()
files.forEach(f => {
  console.log(`${f.filename} - ${(f.size / 1024).toFixed(2)} KB`)
})

9. 删除文件

async function deleteFile(filename) {
  const res = await fetch(`${API_BASE}/files/${filename}`, {
    method: 'DELETE',
    headers: { 'Authorization': `Bearer ${token}` }
  })
  return res.json()
}

// 使用
await deleteFile('index2.html')
console.log('已删除')

10. 获取统计信息

async function getStats() {
  const res = await fetch(`${API_BASE}/stats`, {
    headers: { 'Authorization': `Bearer ${token}` }
  })
  return res.json()
}

// 使用
const stats = await getStats()
console.log(`
📊 统计信息
邮箱: ${stats.email}
子域名: ${stats.subdomain || '未注册'}
网站: ${stats.site_url || '无'}
文件数: ${stats.total_files}
已用空间: ${stats.total_size_mb} MB
`)

完整示例:CLI 风格脚本

// terminal-hosting.js
const API_BASE = 'https://nic.org.digital/api'

class TerminalHosting {
  constructor() {
    this.token = null
  }

  async request(endpoint, options = {}) {
    const res = await fetch(`${API_BASE}${endpoint}`, {
      ...options,
      headers: {
        'Content-Type': 'application/json',
        ...(this.token && { 'Authorization': `Bearer ${this.token}` }),
        ...options.headers
      }
    })
    return res.json()
  }

  async signup(email, password) {
    const data = await this.request('/auth/signup', {
      method: 'POST',
      body: JSON.stringify({ email, password })
    })
    if (data.token) this.token = data.token
    return data
  }

  async login(email, password) {
    const data = await this.request('/auth/login', {
      method: 'POST',
      body: JSON.stringify({ email, password })
    })
    if (data.token) this.token = data.token
    return data
  }

  async registerSubdomain(subdomain) {
    return this.request('/subdomain', {
      method: 'POST',
      body: JSON.stringify({ subdomain })
    })
  }

  async uploadFile(file, filename = 'index.html') {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('filename', filename)
    
    const res = await fetch(`${API_BASE}/upload`, {
      method: 'POST',
      headers: { 'Authorization': `Bearer ${this.token}` },
      body: formData
    })
    return res.json()
  }

  async listFiles() {
    return this.request('/files')
  }

  async deleteFile(filename) {
    return this.request(`/files/${filename}`, { method: 'DELETE' })
  }

  async getStats() {
    return this.request('/stats')
  }

  async getSubdomain() {
    return this.request('/subdomain')
  }
}

// 使用示例
const client = new TerminalHosting()

async function main() {
  // 登录
  await client.login('[email protected]', '123456')
  
  // 注册子域名
  await client.registerSubdomain('myblog')
  
  // 上传文件
  const fileInput = document.getElementById('file')
  if (fileInput.files[0]) {
    const result = await client.uploadFile(fileInput.files[0])
    console.log('上传成功:', result.url)
  }
  
  // 查看统计
  const stats = await client.getStats()
  console.log('统计:', stats)
}

// Node.js 环境(需要 fetch polyfill)
// 浏览器环境直接可用

浏览器控制台快速测试

// 登录
const res = await fetch('https://nic.org.digital/api/auth/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ email: '[email protected]', password: '123456' })
})
const { token, user } = await res.json()

// 保存 token
localStorage.setItem('token', token)

// 获取文件列表
const files = await fetch('https://nic.org.digital/api/files', {
  headers: { 'Authorization': `Bearer ${token}` }
})
console.log(await files.json())

错误处理示例

async function safeRequest(fn) {
  try {
    const result = await fn()
    if (result.error) {
      console.error('❌ 错误:', result.error)
      return null
    }
    return result
  } catch (err) {
    console.error('❌ 网络错误:', err.message)
    return null
  }
}

// 使用
const stats = await safeRequest(() => client.getStats())
if (stats) {
  console.log('统计信息:', stats)
}

完整工作流示例

const client = new TerminalHosting()

async function deploySite(email, password, subdomain, htmlContent) {
  try {
    // 1. 登录
    console.log('1. 登录中...')
    await client.login(email, password)
    
    // 2. 注册子域名
    console.log('2. 注册子域名...')
    const subResult = await client.registerSubdomain(subdomain)
    if (subResult.error) {
      console.log('子域名已存在或无效:', subResult.error)
    } else {
      console.log('✅ 子域名:', subResult.url)
    }
    
    // 3. 创建 HTML 文件
    const file = new File([htmlContent], 'index.html', { type: 'text/html' })
    
    // 4. 上传
    console.log('3. 上传文件中...')
    const uploadResult = await client.uploadFile(file)
    if (uploadResult.success) {
      console.log('✅ 部署成功!')
      console.log('🔗 访问: https://' + subdomain + '.org.digital')
    } else {
      console.log('❌ 上传失败:', uploadResult.error)
    }
  } catch (err) {
    console.error('部署失败:', err)
  }
}

// 使用
deploySite(
  '[email protected]',
  '123456',
  'myblog',
  '<h1>Hello World!</h1><p>My first site on org.digital</p>'
)
3
7
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

7 条回复

排序
  1. YearLater 黄金 5 个月前

    正好不会用API,lz简直是及时雨啊

    1
  2. MSaZs 黄金 5 个月前

    谢谢分享

    1
  3. Gill007 speech_balloon 钻石 2 YEARS 5 个月前

    沪爷你懂得真多

    1
  4. zxylxy 铂金 5 个月前

    来看看

    0
  5. qazxsw 白银 5 个月前

    看不懂

    0
  6. Dudu007 钻石 5 个月前

    OpenAI 🤪

    0
  7. YearLater 黄金 5 个月前

    用的时候发现有点问题,网页端输入的邮箱和密码会自动Lowercase转成小写,也就是说保存的邮箱和密码实际上都是小写的。所以如果之前是在网页端注册的话,调用API的时候要输入全小写的邮箱密码才能登录成功。

    0