How to Create and Delete Applications via Cloudways API

by Ahmed Khan  October 13, 2016

The previous articles in the API tutorial series taught you how to purge Varnish cache using Cloudways API and how you can perform server operations using Cloudways API. In this tutorial, I’ll explain how to create and delete applications on your Cloudways servers using its API.

create-and-delete-applications-via-cloudways-api-banner

Complete code can be found at Application Management via API repository.

Step 1: Install Guzzle

I will use Guzzle to handle all HTTP requests that will be sent to the API. To install Guzzle, run the following command:

installing guzzle

Once the installation finishes, I will create a class which will be our point of interaction with the API.

Step 2: Create the Class to Connect to Cloudways API

Now create a new file and name it CloudwaysAPIClient.php. I will start the code by calling vendor/autoload and define the Guzzle classes.

Now, I will create a class along with the constructor that will load the Guzzle client and then prepare the access token for Cloudways API.

 

The class constructor takes the client email and the API key, and then prepares the token using prepare_access_token(). This function sends the request to Cloudways API for a token. Once the token is received, it is saved in the variable $accessToken. Next, I have created  StatusCodeHandling() function for handling all the status codes  generated by the API.

Next up are the functions that get servers and applications using Cloudways API.

 

In the above functions, I sent requests to Cloudways binding the access token header to get server and applications, decode the information from JSON and return the data.

I will next create the functions which will add and delete applications.

 

In the add application function, I bind the required parameters (server id, application name, application version and application name) and send the request to API in order to add the application.

 

The delete application function takes the serverid and applicationid, bind the parameters and send the request to Application API.

Following is the complete code for CloudwaysClientAPI:

 

The next step is the creation of an application that uses the Cloudways API.

Step 3: Create an Application Using the Cloudways API

First, I will create a new file index.php for the application. The code for the index.php is as follows:

 

In this file, I created an object of the API class and then called the  functions to get the server and the applications. Next, I created a form and several  javascript functions to bind the details coming from the functions.

When executed, the form looks like the following:

php application form

Try adding a new application using the form:

adding php application using api

Once the form has been filled, a notification about the addition of the application will appear:

application added successfully

Next is the code for deleting an application.

Step 4: Delete an Application Using Cloudways API

Create a new file and name it delete.php and paste the following code in it.

 

In this code, I first fetched the servers and the applications and then bind the information to the form and the javascript functions in order to make the form more user friendly. When executed, the code will Let’s will generate the following form:

delete application form

Select the server and the created application in form. Click Delete.

select application to delete

The result is a successful response!.

application deleted successfully

Conclusion:

In this tutorial, I discussed how you could add and delete servers and applications using the Cloudways API. I created a class to connect to the API and get the latest information. Next, came the functions to  add and delete application from the server.

If you have a question about the code or would like to add to the conversation, please leave a comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...