Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Easily Automate The DigitalOcean API From Windows And Mobile

digitalocean API

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.

Delphi is a Native Windows Development tool that modernizes VCL apps for Windows with up-to-date user interface controls, WinRT APIs, and HighDPI-related features. It also supports Android 30 API, AndroidX Libraries, Android support on multiple classes.dex files and integration of external Android Dependencies, making it ideal for DigitalOcean API Automation from Windows and Mobile.

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.

NameType
nameString
regionString
sizeString
imageinteger (if using an image ID), or String (if using a public image slug)
ssh_keysArray
backupsBoolean
ipv6Boolean
private_networkingBoolean
vpc_uuidstring
user_dataString
monitoringBoolean
volumesArray
tagsArray

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)


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Senior Software Engineer | Delphi/FreePascal Enthusiast | Linux/ Unix Enthusiast

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES