DigitalOcean API Skill
Control DigitalOcean infrastructure programmatically: droplets, DNS, databases, storage, networking.
Authentication
API token required. Get one from: https://cloud.digitalocean.com/account/api/tokens
Store in ~/.config/digitalocean/token (just the token, no newline):
CODEBLOCK0
Quick Reference
Droplets (VMs)
CODEBLOCK1
DNS Management
CODEBLOCK2
Firewalls
CODEBLOCK3
Spaces (Object Storage)
CODEBLOCK4
Databases
CODEBLOCK5
Account & Billing
CODEBLOCK6
SSH Keys
CODEBLOCK7
Images & Snapshots
CODEBLOCK8
Regions & Sizes
CODEBLOCK9
DNS Record Types
Supported record types:
- -
A — IPv4 address - INLINECODE2 — IPv6 address
- INLINECODE3 — Canonical name (alias)
- INLINECODE4 — Mail exchange (requires priority)
- INLINECODE5 — Text record
- INLINECODE6 — Nameserver
- INLINECODE7 — Service record
- INLINECODE8 — Certificate Authority Authorization
Common Workflows
Deploy a New Server
CODEBLOCK10
Migrate DNS to DigitalOcean
CODEBLOCK11
Direct API Access
For operations not covered by the script:
CODEBLOCK12
API Documentation
- - Full API reference: https://docs.digitalocean.com/reference/api/
- API v2 base URL: https://api.digitalocean.com/v2/
Common Droplet Sizes
| Slug | vCPUs | RAM | Disk | Price/mo |
|---|
| s-1vcpu-512mb-10gb | 1 | 512MB | 10GB | $4 |
| s-1vcpu-1gb |
1 | 1GB | 25GB | $6 |
| s-1vcpu-2gb | 1 | 2GB | 50GB | $12 |
| s-2vcpu-2gb | 2 | 2GB | 60GB | $18 |
| s-2vcpu-4gb | 2 | 4GB | 80GB | $24 |
| s-4vcpu-8gb | 4 | 8GB | 160GB | $48 |
Common Regions
| Slug | Location |
|---|
| nyc1, nyc3 | New York |
| sfo3 |
San Francisco |
| ams3 | Amsterdam |
| sgp1 | Singapore |
| lon1 | London |
| fra1 | Frankfurt |
| tor1 | Toronto |
| blr1 | Bangalore |
| syd1 | Sydney |
DigitalOcean API 技能
以编程方式控制 DigitalOcean 基础设施:Droplet、DNS、数据库、存储、网络。
身份验证
需要 API 令牌。从以下地址获取:https://cloud.digitalocean.com/account/api/tokens
存储在 ~/.config/digitalocean/token 中(仅令牌,无换行):
bash
mkdir -p ~/.config/digitalocean
echo -n 你的API令牌 > ~/.config/digitalocean/token
chmod 600 ~/.config/digitalocean/token
快速参考
Droplet(虚拟机)
bash
列出所有 Droplet
python3 scripts/digitalocean.py droplets list
获取 Droplet 详情
python3 scripts/digitalocean.py droplets get
创建 Droplet
python3 scripts/digitalocean.py droplets create <名称> --region nyc1 --size s-1vcpu-1gb --image ubuntu-24-04-x64
电源操作
python3 scripts/digitalocean.py droplets power-on
python3 scripts/digitalocean.py droplets power-off
python3 scripts/digitalocean.py droplets reboot
调整 Droplet 大小
python3 scripts/digitalocean.py droplets resize --size s-2vcpu-4gb
快照
python3 scripts/digitalocean.py droplets snapshot --name backup-2024
销毁 Droplet
python3 scripts/digitalocean.py droplets destroy
DNS 管理
bash
列出域名
python3 scripts/digitalocean.py dns list
获取域名记录
python3 scripts/digitalocean.py dns records <域名>
添加记录
python3 scripts/digitalocean.py dns add <域名> --type A --name www --data 1.2.3.4 --ttl 300
更新记录
python3 scripts/digitalocean.py dns update <域名> --data 5.6.7.8
删除记录
python3 scripts/digitalocean.py dns delete <域名>
添加域名
python3 scripts/digitalocean.py dns create <域名>
防火墙
bash
列出防火墙
python3 scripts/digitalocean.py firewalls list
创建防火墙
python3 scripts/digitalocean.py firewalls create <名称> --inbound tcp:22:0.0.0.0/0 --inbound tcp:80:0.0.0.0/0 --inbound tcp:443:0.0.0.0/0
将 Droplet 添加到防火墙
python3 scripts/digitalocean.py firewalls add-droplet id> id>
Spaces(对象存储)
bash
列出 Spaces(需要 Spaces 密钥)
python3 scripts/digitalocean.py spaces list
创建 Space
python3 scripts/digitalocean.py spaces create <名称> --region nyc3
数据库
bash
列出数据库集群
python3 scripts/digitalocean.py databases list
获取数据库详情
python3 scripts/digitalocean.py databases get
账户与账单
bash
账户信息
python3 scripts/digitalocean.py account
余额
python3 scripts/digitalocean.py billing balance
账单历史
python3 scripts/digitalocean.py billing history
SSH 密钥
bash
列出 SSH 密钥
python3 scripts/digitalocean.py ssh-keys list
添加 SSH 密钥
python3 scripts/digitalocean.py ssh-keys add <名称> --key ssh-ed25519 AAAA...
镜像与快照
bash
列出可用镜像
python3 scripts/digitalocean.py images list
列出你的快照
python3 scripts/digitalocean.py images snapshots
删除快照
python3 scripts/digitalocean.py images delete
区域与规格
bash
列出区域
python3 scripts/digitalocean.py regions
列出 Droplet 规格
python3 scripts/digitalocean.py sizes
DNS 记录类型
支持的记录类型:
- - A — IPv4 地址
- AAAA — IPv6 地址
- CNAME — 规范名称(别名)
- MX — 邮件交换(需要优先级)
- TXT — 文本记录
- NS — 名称服务器
- SRV — 服务记录
- CAA — 证书颁发机构授权
常见工作流程
部署新服务器
bash
1. 创建 Droplet
python3 scripts/digitalocean.py droplets create myserver --region nyc1 --size s-1vcpu-2gb --image ubuntu-24-04-x64 --ssh-keys
2. 获取 IP 地址
python3 scripts/digitalocean.py droplets get
3. 添加 DNS 记录
python3 scripts/digitalocean.py dns add mydomain.com --type A --name @ --data
4. 设置防火墙
python3 scripts/digitalocean.py firewalls create web-server --inbound tcp:22:0.0.0.0/0 --inbound tcp:80:0.0.0.0/0 --inbound tcp:443:0.0.0.0/0
python3 scripts/digitalocean.py firewalls add-droplet id> id>
将 DNS 迁移到 DigitalOcean
bash
1. 添加域名
python3 scripts/digitalocean.py dns create example.com
2. 添加记录
python3 scripts/digitalocean.py dns add example.com --type A --name @ --data 1.2.3.4
python3 scripts/digitalocean.py dns add example.com --type CNAME --name www --data example.com.
3. 在注册商处更新名称服务器为:
ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com
直接 API 访问
对于脚本未覆盖的操作:
bash
TOKEN=$(cat ~/.config/digitalocean/token)
curl -H Authorization: Bearer $TOKEN \
-H Content-Type: application/json \
https://api.digitalocean.com/v2/droplets
API 文档
- - 完整 API 参考:https://docs.digitalocean.com/reference/api/
- API v2 基础 URL:https://api.digitalocean.com/v2/
常见 Droplet 规格
| 标识符 | vCPU | 内存 | 磁盘 | 月费 |
|---|
| s-1vcpu-512mb-10gb | 1 | 512MB | 10GB | $4 |
| s-1vcpu-1gb |
1 | 1GB | 25GB | $6 |
| s-1vcpu-2gb | 1 | 2GB | 50GB | $12 |
| s-2vcpu-2gb | 2 | 2GB | 60GB | $18 |
| s-2vcpu-4gb | 2 | 4GB | 80GB | $24 |
| s-4vcpu-8gb | 4 | 8GB | 160GB | $48 |
常见区域
旧金山 |
| ams3 | 阿姆斯特丹 |
| sgp1 | 新加坡 |
| lon1 | 伦敦 |
| fra1 | 法兰克福 |
| tor1 | 多伦多 |
| blr1 | 班加罗尔 |
| syd1 | 悉尼 |