How to Use Cloudways API In 2020 | Is It Really Worth It?

Cloudways API helps you integrate various Cloudways features with several other software to make your life easy. With API you can personalize each software and give your users a better experience. You can try Cloudways APIs with E-commerce stores, development agencies, and almost any kind of website.

If you’re looking to get more information on Cloudways then rollover to check our detailed Cloudways Review.

The APIs in Cloudways is of supreme quality and easy to use as well. Yes, you will require certain coding knowledge for using these APIs especially SQL.

The Cloudways APIs could be used in several software and I will be discussing some of the major Cloudways API integration. 

Read the article and get to know how to integrate the Cloudways API with many software you will require when setting up your website.

General Steps of How to Use Cloudways API

Step 1: Login

First and foremost, you need to login to your Cloudways account and go to the API which is displayed on the top menu of your account dashboard.

Cloudways API- Launch API

Step 2: API key generation

The next screen will ask you to generate an API key in order to use Cloudways API. Go ahead and click on the button which reads the Generate API key.

Cloudways API- Generate API Key

Step 3: Access the documentation and Playground features

After successful API key generation, copy the API key and get access to the Documentation and Playground Cloudways API. 

You can also generate a secret key or generate a new API key if you wish to. You can do this by simply clicking on the Regenerate Key.

You can get all the information on the APIs, how to use them and where to use which one via the Documentation API option. It has all the platforms you can use your API on.

Cloudways API-Documentation

Let us now talk about one of the most significant features- The API Playground.

With Playground API you can test and implement your API integrations with many tools. Keep in mind that the changes you do in the Playground would be reflected on your website and account linked to it. So, it is advised to use it carefully and test the API before using it.

Step 4: Using API Playground in depth

You now need to click on Authorize and then plug in your registered mail ID and remember the API key you copied in step 3? Paste the key here and hit authorize.

Cloudways Platform API

Managing server services using Cloudways API

Managing server services using Cloudways API requires Guzzle PHP HTTP client. This client handles all your HTTP requests.

You need to first install Guzzle via Cloudways.

It can be done in a few simple steps. I will be discussing them here.

Steps Activate Guzzle PHP HTTP 

Step 1: Installing Guzzle

You need to run the command shown below to install Guzzle

The command is: “composer require guzzlehttp/guzzle”

After installation Guzzle automatically creates a class that helps in API interaction.

Step 2: Class Creation to connect Cloudways API 

Now make a new file with the name CloudwaysAPIClient.php and start coding. First off start with autoload and define the classes in Guzzle. Use the following lines of code:

Create a class to load the Guzzle client, also create a constructor and after doing so start preparing the access token which will load Cloudways API.

The following code will trigger the point as discussed above.

What does the above code do?

The above code will take the client’s email and the API along with it and prepare a token using the syntax prepare_access_token(). As soon as you add this syntax to your code, it will send a request to Cloudways API  for token generation. As soon as you receive the token you requested, it will be saved in a variable which you can name anything which you want. I have named it $accesstoken in this case.

Next thing to do it’s to create a StatusCodeHandling() function so that you can handle all the status code that will be generated by the API.

Next you need to code for functions which will get you the servers using the Cloudways API.

Step 3: Creation of an application via Cloudways API

For creating a new application via Cloudways API you need to create an idex.php file.

You can get the code on many websites online. Make sure you refer to those.

The major element of the code will be an object class of API and then you will have to call the function so that it can get the server along with the application.

Have a look at the form which will be created after you successfully create class and call it.

Step 4: Coding for deletion of application via Cloudways API

You need to create a delete.php file for this process. Code according to your needs and get the required results. 

After coding, try deleting the app. The form that you created via the above process should look like the image below.

Let us talk about one major tool you can integrate Coludways API with.

How to create REST API in Silex via User Authentication of Cloudways API

You will primarily need a PHP MySQL hosting to create a REST API in Silex via Cloudways API user authentication.

How does Silex work?

Silex is a type of PHP micro-framework which is built along with Symfony and Pimple components. Its framework words are great for 1-page applications. You can easily integrate Silex with other third party apps or tools because of its extension system which works on the basics of Pimple.

Other than this it makes use of Symfony HTTPKernel to respond and requests the HTTP requests sent by a user to google when they visit your site. Symfony HTTOKernel makes Silex fast and can load the requests efficiently.

So let’s set you up with Silex.

Go to your project page first using the command given below.

Now execute the command- composer require silex/silex “2.0” 

HTTP Code information

There are some error numbers you need to remember while working with the REST API code. All these are HTTP codes and are used frequently or shown frequently when you run your code. Make sure you keep these codes in the back of your mind and do the coding. 

Middleware

You can run your codes and personalize them as well with the help of Middleware. It all depends on the different stages of Silex. You can run your code in such a modified way before or after the matching route.

You can use Middleware in order to see if the user who signed in exists or the username is invalid as well. There are 2 types of Middleware you can use with Silex these are Allipcation and Route.

How to Develop a REST API?

The first step will be to import database tables. These tables should have both user and the messages tables. You can get the code easily online or best visit GitHub for the same.

Next thing you need to do is installation of Eloquent in Silex.

Eloquent works with Silex amazingly, you get beautiful implementation of a working Silex database with Eloquent. Now each table you will be importing for users and messages will have a Model that will be used to interact with the respective table. You can fire a query for data in these tables and along with that you can insert a new column or row, modify the tables, select a primary key or even delete a row.

You need to first install Eloquent and you can do so by running the below code in you composer command prompt.

composer require illuminate/database “5.2”

After installing Eloquent, create a file connection.php so that you can connect it to the database.

You need to also add the information given below in the database to activate it.

Now open the connection.php file and then put the code given below in the image in it. You can get a copyable code from GitHub or many online sites.

Creating User & Message Models

As soon as you install Silex, go to Vendor and then click on composer files. Add scr folder and along with it also add a Todo folder. You can also create 2 folders inside the Todo folders and name them Models and Middleware.

Now you have to create an authentication.php inside the Middleware folder and a message.php inside the model folder along with user.php.

Now add a message class to the message.php folder which will help the Eloquent models.

After successfully adding the message class, you need to authenticate the user first by coding the Middleware. For this, open the authentication.php folder and add the authentication class to the folder.

So, the code uses authenticate(), as the user plugs in the userId given to him/her, authenticate() takes the user’s API from the header section and saves it in the folder. Now this key will be used as an argument by the user model on the basis of which it will be decided whether the user is legit or fake. In case of a fake user, the API key will not be found and will be giving them back a 401 HTTP error message.

You can now add the user class to your code via the user.php.

As discussed, do this step by retrieving the API key information by making use of Eloquent.

The code line 10 from the image i.e., $user= User::where(‘api key’ ‘=’, $apikey)->take(1)->get();

This line generates a similar query that is generated in SQL with the query select * from user where apikey= $apikey and then the code will return the user apikey that will be further used for comparison.

Creation of Route and Perform API Actions

The initialization of Silex via idex.php file is done. We now need to add other parameters like connection file, Model and Middleware along with Request and Response components.

So, let’s get started. Refer the code below to firstly authenticate Middleware before any other coding action.

For authentication, you can use the before application middleware for it, as it allows you to tweak and make changes according to your requirement.

Retrieving Messages of The User

For getting the user messages via the platform, use the get() command. You need to pass the user ID you saved in the previous in this get() command.

Refer to the code below to implement user message retrieval.

You can save your necessary details in a folder. In the above case, I have saved it in the folder named $payload and then I have returned the data that is encoded as well. I have used Postman to test the API. In doing so I am getting the message from the user without the API key and for the test to be successful it should show the error 401 unauthorized users from the user model. It should look something like this.

After the test implementation being successful, pick the API key of the user from the user model, and add the authorization value to it. The user’s messages will now be retrieved instead of an error display.

Now that the experimentation and testing is done, you can retrieve messages from users.

How to Retrieve Messages of a User?

For getting the messages of a user, you just need to pass the message ID with a syntax /message in your payload array, it will retrieve and show the record as well.

Refer to the code below to see how.

You just need to pass the message id in the query where() to retrieve the matching user data.

After successful retrieval, your screen should display text something like the image below.

How to Create a New Message Via Post?

You need to reply to the message you just retrieved from the previous step.  So, do this by getting the message value and then pass it to user ID.

Doing so in Eloquent will help create a new message for the user in the database using the save() function.

The above code will get you the route of the message, i.e. where it was created. The screen should look similar to the image below.

Updating the Message with The Help of Put

Try updating the received message with the help of message value as the parameter and message ID from the route of Silex. Use the code below.

The output should be a screen like an image below.

Message Deletion Via Delete Command

Lastly, you need to code for a deletion message system. Use the delete() command to do so. You need to pass the message ID to the delete() command and the rest will be taken care of by it.

When the command is successful it should give a 204-response code on the bottom right of your screen.

Refer the code below to see how to use the delete command correctly.

Output

Finally, you are done with creating a REST API in Silex via the user authentication method. This method serves a great solution for applications which are cross-device. It also gives you a set of codes such as 201,401,202 etc so that whenever you run code, you come to know if the code is successful or not with the help of the values assigned to these numbers.

Cloudways API is not a free product. It comes with other services that Cloudways gives. Let us have a look at the pricing plans of Cloudways and help you with selecting the best one.

Cloudways Pricing- Which One Should I Go for!

Cloudways has 2 plans with 4 categories each. It has monthly plans and hourly plans as well. If you are just starting off you can start with the hourly plan, get used to the working of Cloudways and its API, and then slowly shift to the monthly plans depending on your need. 

Cloudways-Pricing

Make sure you have a look at all 4 plans both in monthly and hourly classes and select the plan which suits your needs and has the features you require.

The above image shows the plans for DigitalOcean Data Centers. Similar plans are available for Linode data centers, Vultr data centers and Google Cloud data centers. The price is different for all of them. So, make sure which data center you require and choose the plan.

Quick Links:

FAQs

✅What other tools can I integrate with my website via Cloudways API?

You can integrate Codeinteger, Yii2, ToDo apps and many more other than Silex via many different authentications. Check the list on their official website.

✅Are there any yearly plans available for Cloudways?

No, there are no yearly plans to choose from. Cloudways only provides monthly and hourly plans with different rates and databases according to your needs.

✅How is Cloudways customer service?

The customer service provided to the users have been satisfactory so far. There has been no negative experience till now for the majority of customers. They provide you customer service via their website contact page, where you can fill a form, or you can also mail them on their official mail id. The response is quick and they treat all their customers equally.

Conclusion: Fully Utilizing Cloudways API

Using Cloudways API to integrate many tools to your website is a very easy task if you know SQL. You can do several things like personalize your website messages, see who are active as users on your site and also delete the user API key if you want to restrict their entry to your website or a certain part of your website.

The plans, when compared to other similar platforms, is low with the quality of features Cloudways gives you along with the APIs. So, make sure to buy a plan which suits your needs when you need them.

Leave a Comment

Cloudways
0 Shares
Tweet
Share
Share
Pin