How to Make Your Laravel Applications Interact With Cloudways API

by Noor Ali  October 6, 2016

Hey guys, I’m back with some good news for you; Cloudways has finally launched its API for developers to play with. This is a level up for all the developers, giving them more flexibility and more options. Today, we’ll be playing with our Laravel App on Cloudways to interact with Cloudways APIs. This is to give you a brief introduction on using these APIs. So let’s start.

How to Make Your Laravel Applications Interact With Cloudways API

We will use the basic UI which will enable us to access Cloudways API. I will guide you through the whole process, so get yourself a fresh Laravel App on your Cloudways Server and just follow the steps below:

Step-1: Getting your .env file ready

You will see a .env file into your/application/<app_name>/public_html/

Add these following variables into your .env file. You can fetch the API_KEY from here.

Save and Exit

We will implement the following 7 functionalities:

  • List Servers on our Platform
  • Create Server
  • Stop Server
  • Restart Server
  • Restart Varnish
  • Restart Nginx
  • Server Settings

So let’s prepare our routes now:

Step-2: Making our endpoints

Laravel 5.3 has changed a bit when it comes to routes. It could’ve been found in app/Http/ in Laravel 5.2, but now they have been placed in public_html/routes/

So let’s open up our web.php route file.

$vim public_html/routes/web.php

And insert these lines into the file:

Each of our route uses method GET and Calls up the function listServer, stopServer etc. of the controller Server. Let’s make up our controller.

Step-2: Making up our controller

Here goes all our logic, accessing API, and declaring variables etc. Let’s make a file inside app/Http/Controllers/ and name it Server.php

This is how our basic controller looks like. Now, we’ll add our constructor:

Here, we add up the main function that will access the Cloudways API and we setup all the headers necessary for each request. We will use this very function to make all the requests. If we don’t provide it the access token, it will fetch us an access token itself, or else will simply handle our request.

Let’s add our first function for creating a server on cloudways: this will create a default DO server. Let’s add this server before the ending bracket. You can find the complete code at the end of this tutorial.

Let’s add a call for listing all the existing servers on our cloudways account:

Now, we will add a simple call that will stop the server by providing the server ID. You can fetch the server ID from the listServer response. Let’s check that out:

You can see that we have fetched the server ID from the request we get from the UI. We’ll see that later on. A similar call can be implemented to restart the server:

Now, we’ll add two more functions that will call API to restart Varnish and Nginx, they are quite similar, let’s see:

In the end, we need to see what server settings we’re running our servers with. Here it goes:

Step-3: Make our Views
So, here was our controller, lets have a look into our views. I am using default Laravel Welcome view, which resides in resources/views/Welcome.blade.php. I’ll be editing a small part of the code to make it more usable. You can find the link to complete code at the end of this file.

Let’s take a look at our UI:

laravel-cloudways-api-example

Let’s list some servers:

laravel-cloudways-api-example-2

We will get some responses like this. Now you can play with the cloudways API. Here’s the complete code. It’s the git repo, clone it, use it and feel free to add your contributions to it.

I hope you guys learned everything about accessing the Cloudways API. If you found anything difficult, don’t forget to visit the API Documentation for complete details. You can find all the documentation at Developers Portal. You can also leave a comment below and we’ll get back with a solution for you.

Start Creating Web Apps on Managed Cloud Servers Now!

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

About Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...