What is an API?

What is an API?
Photo by Harry Parvin

If you have been working with technology for quite some time, you have probably come across the term API. For those that have been programming and understand how to make a website from scratch, you will be very familiar with the term API.

An API is behind many of the popular functions that you use on websites. It is an acronym that stands for Application Programming Interface. A good illustration would be to think of a recipe to make different kinds of food. Each recipe has step by step instructions on how to create the dish. This is the code that is involved with the API. A computer must follow a set of instructions to accomplish a task.

You can take that recipe and give it to any chef. They will be able to interpret the ingredients and figure out how to combine all the different elements into the finished product. You even have the choice to follow the directions yourself to put together the recipe if you understand cooking and have enough training to use all the tools in the kitchen. An API in the end is just a recipe that the computer understands how to follow.

Developers and APIs

Continuing with our illustration of how an API works, some will bring the recipe to a professional chef that can put together the food quickly and give you whatever dish you order. A developer is like a person that already has the ability to create the recipe and also modify it.

An API is a series of instructions to the computer. For a developer with coding experience, they can tweak some aspects of the API and modify it so that it will meet their needs. This will be beyond the scope of many that just want to plug the API into their site.

Using an API is good for the everyday user because it allows them to get more functionality out of their website without having to create the code on their own. Maybe you would like to display the weather and time for a user that is visiting your website. This content is dynamic and not static. You can’t just put down the weather for one day and leave it. The value is always changing so you need to access an API so that the information updates.

APIs & WordPress

If you’re using WordPress & looking for a way to integrate an API then we recommend that you do it through a WordPress plugin or theme. There are a lot of free and premium plugins that can help you integrate APIs quite easily, however most of them are very limited unless you pay.

If you have coding knowledge yourself then it’s simple to integrate an external API on your WordPress site. For that we recommend this guide by Rapidapi that explains how to integrate external APIs.

Dynamic Content

Whenever you are dealing with dynamic content your first thought should be to find an API that can feed the rapidly changing information to your website. It can also let you interact with popular programs on a deeper level.

Take for example Twitter. This is a popular platform for people that are interested in social media. If you are part of digital marketing, you will want to access things on a deeper level. By using the Twitter API, you can quickly pull information into spreadsheets to break it down and figure out things like the interests of users and how many connections they have. If you are a digital marketer you are going to want to quickly identify people in certain demographics so you know which products to pitch to them. The Twitter API allows you to do these things because you can pull vast sums of information all at once instead of compiling everything by hand.

Improving Your Site

We have briefly looked at how you can improve your business through using an API for a large company. What else can we pull from external websites? Besides loading the weather for a user that is changing dynamically, we can display something that is a little more interesting.

How would you like a website that can display the latest images from Flickr or even Facebook? This would be great content for your users that they are going to be interested in. For this, first locate the API documentation for Flickr. It is important to go through this first in case you run into trouble with integrating the API into your site.

Now you will see the ability to request the format you would like. For many APIs you will have choices such as REST, XML-RPC, and SOAP. For this case, pick REST which is very popular with WordPress sites. When you click on this you will be presented with a Flickr REST API Endpoint.

What is REST?

REST is another acronym to learn if you want to have dynamic and constantly changing content on your site. With REST we can then use GET, POST, and DELETE. This is for any data on Flickr, but it also works for Facebook, Google, or Instagram.

The acronym stands for Representational State Transfer. It was created to distribute hypermedia and first hit the scene with Roy Fielding in 2000. You now can have HTTP requests that will get data for you. HTTP requests can also post data to other websites. The API is the language that two computer programs speak as they talk with each other and communicate. By understanding this language and how it works, you can pull data from almost any site.

API Keys

Websites will not make it simple to pull in data to display on your WordPress site. You first need to make an account. Once you have your account with the website, request an API key from them. Make sure you read the terms of service. There are specific terms that will be different depending on what website you are working with. Some sites will limit how many HTTP GET requests you can make during a certain time frame.

Perhaps you are restricted to just 50 GET requests for a given day. This makes sure that the servers are not constantly being hammered by people looking to make requests on resources. You can increase limits like these many times by going for a Pro version or a commercial version. When you upgrade, you will have access to more HTTP requests that you use to call upon the servers. This will usually be used by a business that needs to make a lot of calls and would not apply to a regular user that is just trying to display new information on their site.

Apply for Permission

You will not always be able to get permission to have an API key. Some sites require that you apply and explain the reason behind why you want access to the key. Companies do not want a huge load placed on their resources unless the developer is actively using the calls to help promote their own products and services. They may even have a team review your request. If you want to get familiar with the technology, request access from a site like Flickr where the requirements are low. Now you will have your key as well as a code along with it.

Authentication and APIs

Next, consider if you want to use the API to get data that requires authentication. For example, if you were to use a Facebook or Instagram API that gives you the ability to post pictures along with text, this would need to be authenticated. Why is this? Well if you think of it this way, you would need to have an account to post to Facebook normally. You can not post anonymously on the site. In a similar way, if an API is posting on your behalf, you need to also be authenticated with a username and password to get in. This will identify you and content posted will link back to you in case things are posted that should not be.

Many systems will use OAuth for authentication since the protocol is easy to use and gives users a simple way to have the API interact with a website as a proxy. Information is sent over a secure link so that the identity of the user is not revealed. You don’t have to always be authenticated though.

Putting it all Together

We now have all the pieces we need to start making API calls. In WordPress, access Appearance and then Widgets. These are written in PHP. Using the documentation for the API that you pulled from the website, paste it in to start making requests. In the case of using the Flickr API, the URL would look like: https://flickr.com/services/rest

In the code, if you see a line for your API key, copy it over from when you requested it and received your password and key. When you get the data back from a site, it might be in a format such as HTML, XML, Text, or JSON. If you are not completely familiar with all of these formats that is fine. Select one that you will be able to work with and be able to interpret later.

Wordpress API
Photo by Harry Parvin

Looking at the Data

Once you are done pasting in the needed information to get the data, you will also need a PHP file or WordPress file that can read the data and output the information onto the screen. Once again, go back to the API documentation on the site for the company you are working with. There should be a block of code that will output the data in a format that you can read as text or have it display as images on your WordPress site.

Formatting the Data

Once you see the data is showing up on your site, it may look strange. The formatting is just the raw data stream. When you make an API call to an external site, there is no attempt to format it in a way that will look pleasing. If you want to shape the data in a way so that it will look presentable, you can use a CSS file that is directed toward polishing everything up. If you have trouble writing the CSS, you can find examples online that will give you some direction. Copy the code into your CSS file so it can be accessed by your WordPress site.

Conclusion

This was a brief tutorial on how to integrate an API with your WordPress site. The topic is complex and can be difficult even when copying code over. Check in with some online sites if you are having trouble and remember to keep coming back to Superb Themes for more in the way of reviews and tutorials.

Leave a Reply

Hi there, all comments are reviewed & your email address will not be published. Let's have an awesome conversation.