![vultrapiheroimage | Embarcadero RAD Studio Delphi C++Builder Blogs Vultr API Hero Image](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/05/vultr_api_hero_image-8384111.jpg?resize=1140%2C694&ssl=1)
The great thing about Delphi is that it makes the tough jobs really easy. For instance, take the task of controlling cloud servers. By using Windows App development tools like RAD Studio Delphi’s cross-platform Firemonkey FMX framework, you can actually EASILY write a small application that allows your users to control cloud-based virtual machines, cloud computing instances, and even full-on “bare metal” cloud servers. What’s more; we’re going to do it in just FIFTEEN lines of code! There are almost no other programming languages around which offer this kind of power in so few lines of code.
Table of Contents
What is Vultr?
Vultr is a commercial company offering a range of cloud-computing resources along the lines of Microsoft Azure, Amazon AWS, and DigitalOcean. We wrote about Digital Ocean before when we accessed their API in the post: Easily Automate The DigitalOcean API From Windows And Mobile
Just like DigitalOcean and many of the other cloud hosting companies, Vultr (https://www.vultr.com) also has a comprehensive collection of APIs to access from our applications.
What are we going to use the Vultr API for?
We’re going to produce some code which allows us to start, suspend/pause and end Vultr virtual instances. The basics of this technique can be used to control other vendor’s offerings too such a DigitalOcean Droplet because the access process to their APIs are very similar.
How do I set up an Application with Vultr’s API?
- Go to the main Vultr website and sign up for an appropriate account which meets your needs. There’s something for everyone there but even the most basic levels support the API.
- Sign in to Vultr Account here https://my.vultr.com
- Go to Account Menu and then the API tab – by default it is disabled, so you have to enable it. Just follow their instructions which are straight-forward.
- Vultr API is using Access Control layer to using our API Key, We have to set the IP subnet to access our API Key, or temporarily set “Allow” to all for development purposes.
- Now, create a new Multi Device Application in your RAD Studio Delphi IDE.
In the previous DigitalOcean post we created a client API Rest connection to control the process of instance creation, deletion and to get all instances. For Vultr you can read the process in their documentation here, but of course you can check full source code here for how to code them.
Since in this article we’re discussing how to Start, Stop or Halt the Vultr instance we have created the first thing we should do Start a Vultr instance.
In the Vultr API documentation the examples says we can call their API as shown in the examples below:
Start a Vultr instance
1 2 3 |
curl "https://api.vultr.com/v2/instances/{instance-id}/start" -X POST -H "Authorization: Bearer ${VULTR_API_KEY}" |
Reboot a Vultr instance
1 2 3 |
curl "https://api.vultr.com/v2/instances/{instance-id}/reboot" -X POST -H "Authorization: Bearer ${VULTR_API_KEY}" |
Reinstall a Vultr instance
1 2 3 4 5 6 7 |
curl "https://api.vultr.com/v2/instances/{instance-id}/reinstall" -X POST -H "Authorization: Bearer ${VULTR_API_KEY}" -H "Content-Type: application/json" --data '{ "hostname" : "Example Instance" }' |
Delphi Vultr API code example
Based on those examples, we can create the Delphi code as follows like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function InstanceCommand(AnInstanceID, ACommand: string; AData: TJSONObject = nil): boolean; var LRestClient: TRESTClient; LRestRequest: TRESTRequest; begin Result := False; LRestClient := TRESTClient.Create(VULTR_API_BASE_PATH + '/instances/'+AnInstanceID+'/'+ACommand); LRestRequest:= TRESTRequest.Create(nil); try LRestRequest.Method := rmPOST; LRestRequest.AddParameter('Authorization', 'Bearer ' + PersonalAccessToken, TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]); if AData <> nil then LRestRequest.AddBody(AData.ToJSON, TRESTContentType.ctAPPLICATION_JSON); LRestRequest.Client := LRestClient; LRestRequest.Execute; Result := True; finally LRestRequest.Free; LRestClient.Free; end; end; |
It’s pretty succinct isn’t it? That’s the power of the Delphi language in action!
What does the Vultr’s Instance sample Windows application look like?
![Screen Shot 20210502 at 204607 | Embarcadero RAD Studio Delphi C++Builder Blogs Vultr API screen shot 2021 05 02 at 20 46 07](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/05/Screen-Shot-2021-05-02-at-20.46.07-8026580.png?resize=300%2C239&ssl=1)
Since we are using Delphi Firemonkey framework you can right-click on the targets in the project Window and compile the code to other supported platforms. This example of using the Vultr API example works for Windows, Android, iOS, macOS, and Linux. Cross platform, Vultr instance control for all desktop and mobile operating systems, in fifteen lines of code.
Check out the full source code here: https://github.com/checkdigits/DelphiVultrInstance_example
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition