DEV Community

Cover image for How to Use DigitalOcean APIs: A Developer's Guide to Cloud Infrastructure
Wanda
Wanda

Posted on • Originally published at apidog.com

How to Use DigitalOcean APIs: A Developer's Guide to Cloud Infrastructure

TL;DR

DigitalOcean APIs let you automate droplets, volumes, firewalls, load balancers, Kubernetes clusters, and more. Use personal access tokens for authentication, hit api.digitalocean.com/v2, and watch for rate limits. For API validation, infrastructure testing, and automated documentation, use Apidog.

Introduction

DigitalOcean focuses on core cloud building blocks: compute (droplets), storage (volumes), networking (floating IPs, firewalls), managed Kubernetes, and the app platform. The API design is straightforward, making automation easy.

Try Apidog today

Developers commonly use the DigitalOcean API to:

  • Spin up dev environments programmatically
  • Manage Kubernetes clusters
  • Automate infrastructure with Terraform or Pulumi
  • Provision resources in CI/CD pipelines
  • Deploy to multiple regions

💡 Tip: Apidog helps test DigitalOcean API calls, save infra configs as templates, and collaborate on provisioning workflows.

Authentication

Personal access tokens

  1. Go to the DigitalOcean Dashboard → API → Generate New Token.
  2. Name your token and set an expiration.
  3. Copy and store your token securely.

Use your token in API calls:

curl -X GET "https://api.digitalocean.com/v2/account" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Response format

{
  "account": {
    "droplet_limit": 25,
    "email": "you@example.com",
    "name": "Your Name",
    "uuid": "abc123xyz",
    "email_verified": true,
    "status": "active"
  }
}
Enter fullscreen mode Exit fullscreen mode

Droplets (Virtual Machines)

List all droplets

curl -X GET "https://api.digitalocean.com/v2/droplets" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Create a droplet

curl -X POST "https://api.digitalocean.com/v2/droplets" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-droplet",
    "region": "nyc1",
    "size": "s-2vcpu-4gb",
    "image": "ubuntu-20-04-x64",
    "ssh_keys": ["ssh-rsa AAAA..."],
    "backups": false,
    "ipv6": true,
    "tags": ["web", "production"]
  }'
Enter fullscreen mode Exit fullscreen mode

Popular sizes:

  • s-1vcpu-1gb — 1 vCPU, 1GB RAM ($5/mo)
  • s-2vcpu-2gb — 2 vCPU, 2GB RAM ($10/mo)
  • s-2vcpu-4gb — 2 vCPU, 4GB RAM ($20/mo)
  • s-4vcpu-8gb — 4 vCPU, 8GB RAM ($40/mo)

Regions:

  • nyc1, nyc3 — New York
  • sfo3 — San Francisco
  • ams3 — Amsterdam
  • sgp1 — Singapore

Get droplet details

curl -X GET "https://api.digitalocean.com/v2/droplets/DROPLET_ID" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Delete a droplet

curl -X DELETE "https://api.digitalocean.com/v2/droplets/DROPLET_ID" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Droplet actions

Reboot:

curl -X POST "https://api.digitalocean.com/v2/droplets/DROPLET_ID/actions" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "reboot"
  }'
Enter fullscreen mode Exit fullscreen mode

Resize:

curl -X POST "https://api.digitalocean.com/v2/droplets/DROPLET_ID/actions" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "resize",
    "size": "s-4vcpu-8gb"
  }'
Enter fullscreen mode Exit fullscreen mode

Snapshot:

curl -X POST "https://api.digitalocean.com/v2/droplets/DROPLET_ID/actions" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "snapshot",
    "name": "my-snapshot"
  }'
Enter fullscreen mode Exit fullscreen mode

Volumes (Block Storage)

Create a volume

curl -X POST "https://api.digitalocean.com/v2/volumes" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "size_gigabytes": 100,
    "name": "my-volume",
    "region": "nyc1",
    "description": "Data volume for web servers"
  }'
Enter fullscreen mode Exit fullscreen mode

Attach volume to droplet

curl -X POST "https://api.digitalocean.com/v2/volumes/VOLUME_ID/actions" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "attach",
    "droplet_id": DROPLET_ID
  }'
Enter fullscreen mode Exit fullscreen mode

List volumes

curl -X GET "https://api.digitalocean.com/v2/volumes" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Networking

List floating IPs

curl -X GET "https://api.digitalocean.com/v2/floating_ips" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Assign floating IP

curl -X POST "https://api.digitalocean.com/v2/floating_ips" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "droplet_id": DROPLET_ID,
    "region": "nyc1"
  }'
Enter fullscreen mode Exit fullscreen mode

Create firewall

curl -X POST "https://api.digitalocean.com/v2/firewalls" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "web-firewall",
    "inbound_rules": [
      {
        "protocol": "tcp",
        "ports": "80",
        "sources": {
          "addresses": ["0.0.0.0/0"]
        }
      },
      {
        "protocol": "tcp",
        "ports": "443",
        "sources": {
          "addresses": ["0.0.0.0/0"]
        }
      },
      {
        "protocol": "tcp",
        "ports": "22",
        "sources": {
          "addresses": ["your-ip/32"]
        }
      }
    ],
    "outbound_rules": [
      {
        "protocol": "tcp",
        "ports": "80",
        "destinations": {
          "addresses": ["0.0.0.0/0"]
        }
      }
    ],
    "droplet_ids": [DROPLET_ID]
  }'
Enter fullscreen mode Exit fullscreen mode

Load Balancers

Create a load balancer

curl -X POST "https://api.digitalocean.com/v2/load_balancers" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-lb",
    "region": "nyc1",
    "algorithm": "round_robin",
    "health_check": {
      "protocol": "http",
      "port": 80,
      "path": "/",
      "check_interval_seconds": 10,
      "response_timeout_seconds": 5,
      "healthy_threshold": 3,
      "unhealthy_threshold": 3
    },
    "forwarding_rules": [
      {
        "entry_protocol": "http",
        "entry_port": 80,
        "target_protocol": "http",
        "target_port": 80
      },
      {
        "entry_protocol": "https",
        "entry_port": 443,
        "target_protocol": "https",
        "target_port": 443,
        "tls_passthrough": true
      }
    ],
    "droplet_ids": [DROPLET_ID_1, DROPLET_ID_2]
  }'
Enter fullscreen mode Exit fullscreen mode

Kubernetes Clusters

Create a Kubernetes cluster

curl -X POST "https://api.digitalocean.com/v2/kubernetes/clusters" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-cluster",
    "region": "nyc1",
    "version": "1.28",
    "node_pools": [
      {
        "name": "worker-pool",
        "size": "s-2vcpu-4gb",
        "count": 3,
        "auto_scale": true,
        "min_nodes": 2,
        "max_nodes": 6
      }
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

List node pools

curl -X GET "https://api.digitalocean.com/v2/kubernetes/clusters/CLUSTER_ID/node_pools" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Scale node pool

curl -X PUT "https://api.digitalocean.com/v2/kubernetes/clusters/CLUSTER_ID/node_pools/POOL_ID" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "count": 5
  }'
Enter fullscreen mode Exit fullscreen mode

Delete cluster

curl -X DELETE "https://api.digitalocean.com/v2/kubernetes/clusters/CLUSTER_ID" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Testing with Apidog

Infrastructure provisioning can be costly—always test your API flows before creating resources.

Apidog test UI

1. Environment setup

Set your environment variables in Apidog:

DIGITALOCEAN_TOKEN: your_token
BASE_URL: https://api.digitalocean.com/v2
DEFAULT_REGION: nyc1
DEFAULT_SIZE: s-2vcpu-4gb
Enter fullscreen mode Exit fullscreen mode

2. Validate responses

Use test scripts to check API responses:

pm.test('Droplet created successfully', () => {
  const response = pm.response.json()
  pm.expect(response.droplet).to.have.property('id')
  pm.expect(response.droplet.status).to.eql('new')
})

pm.test('Token is valid', () => {
  const response = pm.response.json()
  pm.expect(response.account).to.exist
  pm.expect(response.account.status).to.eql('active')
})
Enter fullscreen mode Exit fullscreen mode

3. Dry run concepts

DigitalOcean doesn't support dry runs, but you can validate request bodies:

const validRegions = ['nyc1', 'sfo3', 'ams3', 'sgp1']
const validSizes = ['s-1vcpu-1gb', 's-2vcpu-2gb', 's-2vcpu-4gb']

pm.test('Region is valid', () => {
  const requestBody = JSON.parse(pm.request.body.raw)
  pm.expect(validRegions).to.include(requestBody.region)
})

pm.test('Size is valid', () => {
  const requestBody = JSON.parse(pm.request.body.raw)
  pm.expect(validSizes).to.include(requestBody.size)
})
Enter fullscreen mode Exit fullscreen mode

Test DigitalOcean infrastructure APIs with Apidog — free for developers.

Common Errors and Fixes

401 Unauthorized

  • Cause: Invalid or expired token.
  • Fix: Regenerate your token and check your Authorization header.

422 Unprocessable Entity

  • Cause: Invalid parameters (wrong region/size/image).
  • Fix: Confirm valid values in the API docs. Common issues:
    • Region doesn't support the size
    • Image ID does not exist
    • Droplet limit reached

429 Too Many Requests

  • Cause: Rate limit exceeded (default 2000 req/hr).
  • Fix: Implement exponential backoff:
async function doRequest(url, options, retries = 3) {
  for (let i = 0; i < retries; i++) {
    const response = await fetch(url, options)
    if (response.status === 429) {
      await sleep(Math.pow(2, i) * 1000)
      continue
    }
    return response
  }
  throw new Error('Rate limited')
}
Enter fullscreen mode Exit fullscreen mode

Droplet limit reached

  • Cause: Account has too many droplets.
  • Fix: Delete unused droplets or request a limit increase from support.

Alternatives and Comparisons

Feature DigitalOcean AWS GCP
Droplet sizes Fixed Custom Custom
Kubernetes Managed DOKS EKS GKE
Object storage Spaces S3 Cloud Storage
Block storage Volumes EBS Persistent Disk
Load balancers Built-in ELB Cloud LB
Free tier $200 credit Limited $300 credit
API simplicity ★★★★★ ★★☆☆☆ ★★★☆☆

DigitalOcean wins on API simplicity—most operations are direct, without deeply nested services.

Real-World Use Cases

Development environments:

Automate branch-based environments: every PR creates a droplet, runs tests, and destroys it after merging. This gives devs production-like test beds automatically.

Auto-scaling web servers:

Monitor server load, and scale droplets up/down using the API. Attach/detach droplets from load balancers as needed for cost-efficient scaling.

Database clusters:

Provision primary and replica volumes across regions using the API. Automate replication, backups, and failover.

Conclusion

Here's what you can do with the DigitalOcean API:

  • Authenticate with personal access tokens
  • Automate droplet lifecycle (create, manage, delete)
  • Attach and manage block storage volumes
  • Set up firewalls and load balancers
  • Manage and scale Kubernetes clusters
  • Test and validate infrastructure with Apidog before deploying

FAQ

How much does a droplet cost?

Starts at $5/month for 1 vCPU/1GB. See the pricing page for latest rates. Hourly billing available.

Can I use SSH keys with API-created droplets?

Yes. Pass the SSH key fingerprint in the ssh_keys array when creating droplets.

What's the difference between volumes and Spaces?

Volumes = block storage for droplets. Spaces = object storage (like S3). Use volumes for databases, Spaces for files.

How do I get the Kubernetes config?

curl -X GET "https://api.digitalocean.com/v2/kubernetes/clusters/CLUSTER_ID/kubeconfig" \
  -H "Authorization: Bearer YOUR_TOKEN"
Enter fullscreen mode Exit fullscreen mode

Can I resize a droplet?

Yes, use the resize action. Downgrade requires power-off; upgrade works live.

What's the difference between backups and snapshots?

Backups = automatic, managed by DigitalOcean. Snapshots = manual, on-demand.

How long do droplets take to create?

Usually 30–60 seconds, but can vary by region/size.

Can I use Terraform with DigitalOcean?

Yes, there’s an official Terraform provider—ideal for infrastructure as code.

Top comments (0)