Quickly Deploy An API – Using Apigility

Phil March 6, 2018
0 people like this post
Quickly Deploy an API

In a previous blog post I discussed building a custom CMS for a business a few friends and I had started (the business is still going, you can find our website here). The development of our CMS had gone well. I managed to get the CMS live and our business was using it to populate the database. Now, I had to expose all the data via an API. This would allow me to be able to develop our mobile apps, displaying and processing our data. Could I quickly deploy an API allowing me to move onto the mobile development?

API’s are everywhere. Most new products or services that come to market have an API that a developer can connect to and use. I’ve even worked for a startup that built the API first. The startup used this API to perform almost all data processing for their website and mobile applications. All new development work was implemented in the API first. Validated. Then passed onto the web and mobile team so they could use it.

There are probably a million and one ways to build an API. I wanted to get something out there quickly. At the time I had just been to a developer conference called PHP North West. At the conference someone had demonstrated a new product called Apigility. Could this be the answer?

Quickly Deploy an API

Quickly Deploy an API

So, would Apigility allow me to quickly deploy an API? As our products were not yet live. I was using our production server to install and configure the API. I know a bit risky. Time was of the essence. We had a goal to get the mobile apps out to the world by August 2012. At the time I had around 9 months to build the API and both an Android and iOS app. All in my spare time. So I did a bit of corner cutting.

Apigility is really easy to install. It is a case of running one command on your Linux server to get the package installed. After the installation had completed. I created a new website and URL pointing to the installation allowing me to begin constructing the API.

Normally to create an API, you either build on top of a framework or start developing from scratch. Both options can lead to a lot of coding. Apigility has been designed to allow you to build an API using a web interface without having to write any code. I won’t go into all the options that are available. The Getting Started section of Apigility documentation gives you a great overview of what you can achieve with their software.

Getting The Data

I had designed our database to be straight forward. Nothing crazy. For the API it would be a case of creating a new endpoint which would return data from a specific database table. Simple. For example the ‘/leagues’ endpoint of the API would just return all the data from the leagues database table. I was hoping that implementing this using Apigility would be as straight forward.

I was in luck. Apigility has an option which allows you to connect your API to a database. I managed to easily connect to our database. Next I created a ‘New Service’ in my new API for the leagues endpoint. As this service only needed to return data, I set this as a GET request. The Apigility interface then listed which database tables were available for me to connect to. I selected the leagues table and clicked save. Job done?

Right. Did it work. I opened a piece of software called POSTMAN. Which is used to connect to API’s allowing you to validate that your API is working and that the returned data is correct. I inputted the URL for my API and the new leagues endpoint and clicked ‘SEND’. Wahoo, well what do you know it worked. What a result within an hour or two I had an MVP API which our mobile apps could use.

Getting A Single Record

So far I had managed to create a few endpoints which would return all the data from certain database tables. What if I needed the API to return a single league record? This wasn’t as straight forward as returning all the data. I was hoping it wouldn’t be too difficult and I could use the web interface to achieve this. There was a little bit of tweaking needed in the interface, but it was fairly straight forward. You need to tell Apigility that if our endpoint looks like ‘/leagues/{id}’. Then the value in {id} needs to link to a field in our database. The API will then query the database for a record matching the value {id} and return the data.

Back to POSTMAN. Again I specified the API URL and added the value ‘1’ to the leagues endpoint. I was hoping this would return data on the Premier League record. YES, it worked. A single record was returned and the data was what I was expecting.

Fantastic. The API was doing what I needed it to AND I hadn’t had to write a single line of code yet. So far it took me ONE EVENING to install, config and implement the basic structure of the API.

Challenges

Unfortunately it wasn’t all plain sailing using Apigility. If I wanted my endpoint to return all records, or return a single record by an id from a database table then it worked fine. The issue I came across was when trying to load multiple records that are linked to an id. Also if I wanted to filter by multiple fields. For example, to load all clubs for league ID 1 and have that an active flag of ‘Yes’. I was having real trouble getting this working.

Apigility is built on the Zend Framework. I have discussed using this framework in a previous blog post in which I stayed away from Zend as I felt it over complicated things. After some searching on the internet I found a blog on how to customise Apigility to allow you to pass in multiple parameters as part of an API call and return the data from the database as needed. This meant delving into the Zend Framework.

I won’t go into massive detail of how you can do this. If you want to know more about how I implemented a work around please drop me a message. Under the hood Apigility creates a number of files when you create a new service endpoint in the web interface. To achieve what I needed, it was a case of editing these files and adding a new file. Basically get back on the tools and do a bit of coding. In these files you check the URL query string for an expected value that you determine. You then extract information from that value, which you can then tell the Zend Framework to use when querying the database to return the data you require.

Implementing The Workaround

Once I got my head round what I needed to do, I didn’t find it too difficult. It was a bit of work to implement it into each endpoint that I needed to filter on. It was a case of making sure 3 files existed in the endpoint. Edit one of the files to add a few lines of code to check for the filter and pass the values onto the database connector. I also had to update a few lines of code in the configuration file of the API to tell Apigility about the updated structure of each endpoint.

As always there may be a better way to do this. It is probably my lack of knowledge around Apigility and the Zend Framework that I had to make these changes to get it working as I would have liked.

That said, the workaround did do what I needed. I could load specific information using a more complicated querying system to enhance the data flow within our mobile apps.

Security

Securing an API should be something do very early on in the development process. Yes, it is a boring subject. It is something you can’t avoid anymore if you want to protect your data. Apigility has numerous ways you can lock down and require authorisation to secure your endpoints. Selecting which method to use will be down to how your API is to be consumed. Again, Apigility’s documentation should help you decide how and what security to implement.

Even before I started integrating the API into our mobile apps. Once I was happy that I was exposing the correct data. I began to implement the security measures so that no system could talk to the new endpoints without Authorisation. This saved me loads of development time as I didn’t have to go a refactor any code once the API had been secured.

Thumbs Up or Thumbs Down

Using Apigility to quickly deploy an API was reasonably straight forward. I managed to construct an API returning all the data that would be required for our mobile apps to work. Could it be done without any coding? No. That said it wasn’t a massive amount and using Apigility did cut down the number of hours it took me to build the API.

Would I recommend using it? Tough one, it depends. If you are building a basic API then Yes I would recommend it. The “out of the box” functionality is fantastic. If you need additional complexity or business logic behind an endpoint it could get tricky. Especially if like me you haven’t had much exposure to the Zend Framework. Then I would recommend other avenues.

If you would like to know more about the projects that I have used Apiligity on. Or have any questions please get in touch. Cheers

Category: Tech
  • 0
  • 77
Phil

    Leave your comment