Getting started overview
Getting started with the DigitalOcean API involves a sequence of steps that enable programmatic control over cloud infrastructure. This guide outlines the essential actions: account creation, API token generation, and executing an initial API request. The DigitalOcean API provides a RESTful interface to manage resources such as Droplets (Virtual Machines), Kubernetes clusters, and Managed Databases, among others. Developers often use the API for automation, infrastructure as code, and integrating DigitalOcean services into custom applications.
The primary method for authenticating with the DigitalOcean API is through Personal Access Tokens (PATs), which adhere to the OAuth 2.0 specification for token-based authorization. These tokens grant specific permissions to interact with your DigitalOcean account resources. The API is designed to be developer-friendly, offering comprehensive documentation and official SDKs in multiple programming languages, including Go, Ruby, Python, PHP, and JavaScript, to streamline development workflows. Examples in this guide will use cURL for simplicity, demonstrating direct HTTP interactions.
To summarize the quickstart process, follow these steps:
| Step | What to Do | Where |
|---|---|---|
| 1. Account Setup | Create a DigitalOcean account. | DigitalOcean Sign Up Page |
| 2. Generate Token | Create a Personal Access Token (PAT). | DigitalOcean API authentication docs |
| 3. Make Request | Execute a GET request to list your Droplets. |
Terminal/Command Prompt |
| 4. Explore Further | Review API reference and SDKs. | DigitalOcean API Reference |
Create an account and get keys
Before making any API calls, you need an active DigitalOcean account. If you do not have one, navigate to the DigitalOcean sign-up page and follow the prompts to create an account. DigitalOcean offers a free tier for select products, which can be utilized for initial API exploration without incurring immediate costs.
Once your account is set up, the next critical step is to generate a Personal Access Token (PAT). This token serves as your API key, authenticating your requests to the DigitalOcean API. To generate a PAT:
- Log in to your DigitalOcean account.
- Navigate to the "API" section in the left-hand sidebar of the control panel.
- Click the "Generate New Token" button.
- Provide a descriptive name for your token (e.g.,
my-first-api-token). - Select the scopes (permissions) for the token. For initial read-only testing, selecting only the "Read" scope for all resources is sufficient. For managing resources, "Write" scopes will also be necessary. For a Droplet listing, the
droplet:readscope is required. - Click "Generate Token".
- Crucially, copy the generated token immediately. DigitalOcean will only display the token once. If you lose it, you will need to revoke it and generate a new one. Store this token securely, as it grants access to your DigitalOcean resources.
For more detailed instructions on token generation and management, refer to the DigitalOcean API authentication documentation.
Your first request
With your Personal Access Token in hand, you can now make your first API request. This example demonstrates how to retrieve a list of all Droplets configured in your account using the /v2/droplets endpoint. Replace YOUR_API_TOKEN with the token you generated.
Open your terminal or command prompt and execute the following cURL command:
curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
"https://api.digitalocean.com/v2/droplets"
Upon successful execution, the API will return a JSON response containing an array of Droplet objects, if any exist in your account. Each object will include details such as the Droplet's ID, name, region, IP addresses, and status. If you have no Droplets, the droplets array in the JSON response will be empty.
{
"droplets": [
{
"id": 12345678,
"name": "my-web-server",
"memory": 1024,
"vcpus": 1,
"disk": 25,
"region": {
"slug": "nyc1",
"name": "New York 1",
"features": [
"private_networking",
"backups"
]
},
"status": "active",
"created_at": "2023-01-01T12:00:00Z",
// ... more droplet details
}
],
"links": {},
"meta": {
"total": 1
}
}
This successful response confirms that your API token is valid and correctly configured, and you have established communication with the DigitalOcean API. For a comprehensive list of available endpoints and their specific parameters, consult the DigitalOcean API Reference.
Common next steps
After successfully making your first API call, you can explore various functionalities offered by the DigitalOcean API. Common next steps include:
- Creating Resources: Use
POSTrequests to provision new resources, such as creating a new Droplet (create Droplet API endpoint) or setting up a new Kubernetes cluster. - Managing Resources: Utilize
PUTandDELETErequests to modify existing resources (e.g., resizing a Droplet) or remove them (e.g., destroying a Droplet when no longer needed). - Monitoring and Automation: Integrate API calls into scripts or applications for automated deployment, scaling, monitoring, and backup solutions for your infrastructure.
- Exploring SDKs: If you are working with a specific programming language, consider using one of the official DigitalOcean SDKs (Go, Ruby, Python, PHP, JavaScript). SDKs abstract the HTTP request details, providing language-native methods for interacting with the API, which can simplify development.
- Webhooks: Set up DigitalOcean webhooks to receive real-time notifications about events in your account, such as Droplet creation or status changes, enabling event-driven automation.
- Cloud Infrastructure Automation: Beyond basic resource management, the API supports advanced use cases like configuring Virtual Private Clouds (VPCs), managing Load Balancers, and interacting with Spaces Object Storage.
For more inspiration on what you can build, explore the DigitalOcean Community Tutorials, which often include API-driven examples.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting tips:
- Unauthorized (401) Error:
- Incorrect Token: Double-check that you copied the Personal Access Token correctly. PATs are long strings of characters.
- Missing Bearer Prefix: Ensure the
Authorizationheader is formatted asBearer YOUR_API_TOKEN, with "Bearer" followed by a space and then your token. This is a common requirement for Bearer Token authentication. - Expired or Revoked Token: Confirm your token is still active in your DigitalOcean account API settings.
- Forbidden (403) Error:
- Insufficient Scopes: The token might not have the necessary permissions (scopes) to access the requested resource. For listing Droplets, the
droplet:readscope is required. Review your token's scopes in the DigitalOcean control panel and regenerate if needed with broader permissions.
- Insufficient Scopes: The token might not have the necessary permissions (scopes) to access the requested resource. For listing Droplets, the
- Not Found (404) Error:
- Incorrect Endpoint URL: Verify that the URL
https://api.digitalocean.com/v2/dropletsis typed correctly. Any typos in the base URL or endpoint path can lead to this error.
- Incorrect Endpoint URL: Verify that the URL
- Bad Request (400) or Unprocessable Entity (422) Errors:
- These typically occur with
POSTorPUTrequests when the request body is malformed or contains invalid data. Ensure your JSON payload is valid and adheres to the API's expected schema for the specific endpoint.
- These typically occur with
- Network Issues: Check your internet connection. If you are behind a corporate firewall, ensure that requests to
api.digitalocean.comare not being blocked.
If you continue to experience issues, consult the DigitalOcean API documentation for specific error codes and troubleshooting guides, or reach out to DigitalOcean support.