Easily Automate The DigitalOcean API From Windows And Mobile

DigitalOcean offers a set of APIs that we can access to manage their products from our application.  You can create applications that act like DigitalOcean’s Droplets Control Panel. You can access DigitalOcean’s API documentation here https://developers.digitalocean.com/documentation/v2/. One reason to utilize the DigitalOcean API is to automate your cloud server management and deployment process. If you are building out separate machines for customers (using RAD Server for example) this would be a great way to automate that process. Or maybe you have a CI pipeline you’re setting up that involves automated deployment and testing of an installation process this would be a great way to automate the process through Delphi and the DigitalOcean API.

How do I set up a Windows app with DigitalOcean’s API?

This article is only an example of the initial steps in creating an automation application, so I only show how to set up the DigitalOcean’s Droplet.

In order to create a droplet (virtual private server) the /v2/droplets endpoint exists. It can be called to create a new server for you to use. Here are the two request headers needed which include authorization.

Here are the list of request parameters that can be set when creating a droplet. The parameters are sent via JSON in the request body. You will see these parameters used in the Object Pascal code below.

Name Type
name String
region String
size String
image integer (if using an image ID), or String (if using a public image slug)
ssh_keys Array
backups Boolean
ipv6 Boolean
private_networking Boolean
vpc_uuid string
user_data String
monitoring Boolean
volumes Array
tags Array

And here is some example JSON of a request body:

What Does the Delphi API code look like to access DigitalOcean?

We are going to use the TRESTClient TRESTRequest TRESTResponse pipeline that is built into the Delphi RTL to make it easy to handle the REST request. The library is available for all platforms that are supported by Object Pascal. The REST Library framework focuses on JSON as the representation format.

REST.Client.TRESTClient is the component that actually executes a request to a service. TRESTClient manages the HTTP connection to the service, deals with HTTP headers and proxy servers, and receives the response data. Authenticators (described later) can be attached to the client for a convenient way of adding authentication to a request.

The request (REST.Client.TRESTRequest) holds all parameters and settings that form the actual HTTP request to the service. When connected to a client-component, it can be executed (even at design time). One of the most important properties of the request is the ‘Resource’. This value forms the previously mentioned ‘second half’ of the full request-url and defines the actual action that is executed on the service. The request also defines the HTTP methods (‘get’, ‘post’, ‘put’, ‘delete’) that are used for its execution.

The response holds all the returned data from the service. The data includes the HTTP status code, error messages (if any exist), and of course the returned JSON data. A response object can be created on the fly by the client while executing a request, or the response object can be created beforehand and then connected to a client before executing a request.

We need to create 3 main methods:

First, the “CreateDroplet” API method

Now, the “DeleteDroplet” API method

Finally, code to access the “GetAllDroplets” method

What does the DigitalOcean sample Windows application look like?

DigitalOcean API in action image

Since we are using Delphi Firemonkey framework you can compile to other supported platforms also, like Android, iOS, macOS, Windows, and Linux.

Check out the full source code here (GitHub link)