How do I use the API with my Cloud Hosting?

Ruth Turner
Published: 13 February 2024Last updated: 13 February 2024
Share:

It’s possible to set up your new Cloud Hosting using the Reseller API – doing so can allow you to automate functionality such as creating packages, activating services or resetting your cloud profiles. A full list of available endpoints, as well as full code examples for each is available within our API Documentation.

Below is a couple of examples using PHP to call and utilise the Reseller API. All of these are using our PHP API Wrapper, which can be downloaded here.

Once downloaded, use Composer to download the necessary vendor files with composer update, and then you should be set to start making use of the API. Requirements for the API Wrapper are included in the Read Me file.

Note: Reseller Hosting is required in order to access and utilise the API.

How do I fetch the ID of my Cloud Server using the API?

First off, we want to use the API to check the details of our Cloud Server(s). We can do this using the following endpoint:

GET /cloud_server

The below code is how we’d call for the endpoint using PHP:

PHP

Request:

<?php
require_once "vendor/autoload.php"; //We specify the API wrapper location.
$bearer_token = "YOUR API KEY"; //Your 20i API key.
$services_api = new \TwentyI\API\Services($bearer_token);
$response = $services_api→getWithFields("/cloud_server");

Result:

The response will contain details on your Cloud Server with an array, including the ID. Make note of that ID, as you’ll need it to make requests to that specific Cloud setup, such as for creating a package. For example:

Array
(
[0] => stdClass Object
(
[configuration] => stdClass Object
(
[CpuCores] => 1
[RamMb] => 1024
[OsDiskSizeGb] => 25
[DataDiskSizeGb] => 0
[Provider] => 20i
[Zone] => 20i
[Country] => GB
[VpsOsId] => 1
[spec] => micro
)
[externalId] => 1111
[location] => dc_location-uk
[name] => vps-123456.mvps.stackcp.net
[productSpec] => cloud-20i
[count] => INF
[cpanel] => 
[cpanelInfo] => 
[cpanelRebuilding] => 
[id] => 1111
[isCloud] => 1
[isManaged] => 1
[packageIds] => Array
(
)
[profileId] => 1
[timelineService] => normal
)
)

How do I create a package on my Cloud Server using the API?

Now that we have our ID, we can now make use of it to create our first package. We can do this using the following endpoint:

POST /cloud_server/cloudServerId/addWeb

The below code is how we’d call for that endpoint using PHP:

PHP

Request:

<?php
require_once "vendor/autoload.php"; //We specify the API wrapper location.
$bearer_token = "YOUR API KEY"; //Your 20i API key.
$services_api = new \TwentyI\API\Services($bearer_token);
$domain = "myclouddomain.com"; //The primary domain we wish to assign to our new package.
$extra_domain_names = []; // Any extra domains we wish to assign to the package as an array.
$addPackage = ["domain_name" => $domain, "extra_domain_names" => $extra_domain_names]; 
// The constructed payload for the endpoint, containing the domains and extra domains for the package.
$response = $services_api->postWithFields("/cloud_server/4838/addWeb", $addPackage);

Result:

The response will contain the ID of your newly created package on your Cloud Server, returned as an object:

stdClass Object
(
[result] => 1234567
)

And your new package should show in the Manage Cloud Hosting area of My20i, ready to use. 

Further endpoints and code examples are available within the API Documentation available in My20i for your perusal, and the above should help you with getting started with this approach to Cloud Server hosting and provisioning – happy coding!