Harnessing third-party APIs: advanced WP strategies

May 30, 2024
Third party APIs on

-sidebar-toc>

In this blog post we'll discuss the benefits of using third-party APIs with WordPress. The article will explore the different kinds of APIs available, tips on how to choose the best one and much more. The main part of the article will discuss the ways to utilize third-party APIs using a hosted WordPress website.

There's a lot to get through, so let's get started!

What third-party APIs are

An Application Programming Interface (API) is a set rules and protocols to let different applications communicate and collaborate. APIs function as intermediaries because they allow you to connect particular data from other services.

A detailed view of Node.js code displaying how to read an unoptimized JPEG file using the filesystem module, and optimize it using TinyPNG's Tinify API.
A snippet of code of TinyPNG's Tinify API.

If you make use of a platform's API to access the endpoints it provides, this is considered first-party. But accessing an API of the same service in conjunction with another one makes it a third-party. For example, you might access the endpoints for the OpenWeatherMap's Weather API inside your WordPress site's programming.

The OpenWeather site detailing its Weather API, specifically the One Call API 3.0. It mentions the API's capability for historical weather data and daily aggregation, alongside subscription details including a free tier and a ‘pay as you call’ pricing model.
The OpenWeather Weather API page.

As you can expect, this could expand the possibilities for what you can achieve with WordPress or your website.

The benefits from using APIs developed by third parties in conjunction with WordPress

There's more to it than that However, generally speaking, APIs will provide you with greater power and flexibility than WordPress and its plugins can give you.

Here are some other key advantages:

  • This will save you developing time. It's quicker to utilize an API that is already in use by third parties for building out the functionality instead of starting completely from the ground up. This will help you save time and let you focus on the other areas of your project.
  • You're able to personalize the User Experience (UX). You can create any functionality users want to see in the web application to meet your specific requirements using third-party APIs.
  • Maintenance isn't the responsibility of you. The developer of the API of a third-party vendor will keep it in good condition and upgrade it. You'll have access to the latest capabilities and enhancements without having to invest additional funds.

Third-party APIs are valuable whenever you require supreme integration, customization, and performance. Of course, this will be the only solution when there's an existing WordPress plugin available to meet your needs.

However, in general it is the case that users need to personalize the functionality by changing the parameters, a plugin will be more effective. But, there's plenty of ways to integrate functions into your website through third-party APIs, too.

Types of APIs from third parties are available.

The market for APIs in general it is massive. An Google Cloud report notes that around 60 percent of internet apps make use of APIs. In the same report, it is noted that the majority of leaders believe APIs aid in the development of better products.

Even though it can be difficult to assess the true worth of APIs from third parties, there is no doubt that they serve several subjective applications. Additionally, third-party APIs are able to meet different needs and functionalities:

  • Analytics APIs. You have plenty of choices when it comes to analytics. Google's APIs have been a hit for this, and a lot of site administrators are looking to integrate Lighthouse and Analytics data into WordPress.
  • Weather APIs. With APIs from websites such as OpenWeatherMap, Weatherbit, you can provide real-time weather data. Given the complexity you need you can add things like geolocation as well.

There isn't an exhaustive list of API types, and there are countless additional APIs to choose from for various reasons. There are a lot of options to choose from, deciding on the most suitable API to connect is crucial. We'll do that next before we explore ways to integrate third-party APIs in WordPress.

What is the best third-party APIs for your needs

Similar to the list of choices when you choose the best WordPress plugin, it's likely that you'll do the same when choosing the appropriate API from a third party. It's important to choose your APIs carefully, as this will determine the direction your website will take.

Your primary concern is to determine what features your website requires. This may sound obvious, but you should seek out APIs that have an appropriate mix of features and functionality for you.

However, there are other factors to keep in mind when choosing third-party APIs:

  • Prices. Not all APIs are free. Certain APIs offer free tiers but that have limitations. Other APIs have a pricing structure that is premium. So, be sure you align your budget, the API cost, your expected usage, and access.

An API usually is a long-term solution, which means it also has to work regardless how big your website. Let's look deeper at this today.

Scalability

The API should be able to provide no matter how much traffic your site sees. It's sometimes difficult to determine if an API can deliver. It may need you to take an educated guess based on how the API provider describes its offering.

An infographic explaining web hosting scalability, contrasting a non-scalable single server setup with a horizontally scalable setup using multiple servers and a load balancer.
Visually, there is a difference between a non-scalable setup as well as a more general-purpose horizontal scaling setup. ( Image Credit: Nithin Mohan)

It is also important to consider whether an API price limits your requests to. Over the long term it will provide you with an improved, consistent and secure service, even with high load.

But, you'll need to consider all of the factors carefully. Selecting the appropriate third-party API that meets your needs will be a advantage when looking to integrate them with WordPress.

Tips and practices to incorporate third-party APIs in WordPress

Integration of an API in your website is usually simple. However, you need a systematic approach to make sure it's a seamless integration.

Your first act should be to go through the complete manual. The API's documentation should clearly define the features that you can play around with. In addition, it should provide details on accessible endpoints, parameters as well as authentication methods.

The  documentation page for its API, outlining the necessary prerequisites for using it. The information includes having a WordPress site, and details on common use cases such as reporting and site management.
The API Documentation

But, there are lots of tips and techniques to be successful in connecting your API in conjunction with WordPress:

  • handling authentication. Most APIs need authorization credentials to connect their services. If you don't have it, you will not be able to access the API's endpoints.
  • Making API calls. WordPress gives you numerous ways to make API calls. One of them is the integrated HTTP API. It's worth taking the time to choose the right technique that meets your preferences. Again, the API documentation can help in this regard.
  • Parsing API response. APIs will return responses in various formats, for example, JSON and XML. As such, you'll have to parse and handle those API responses in your application to get and utilize the data.
  • dealing with errors. Using proper error handling techniques can allow you to handle API errors with grace. Also, you can display meaningful error messages to the visitors to your site.
  • Tests and debugging. As with any app or implementation, test your API integration to make sure it functions exactly as you would expect. Tools such as Postman or the browser's developer tools are able to help you test API calls, and also debug issues.

It is also important to track the API use. Because costs usually relate to an allowance for usage It is essential to keep within those boundaries. We'll then explain how the tools can help with this.

Making preparations for WordPress and integrating with third-party APIs

Before you integrate third-party APIs with WordPress is crucial that you have both the platform and can handle the task. The configuration of both your CMS and host might often be necessary, especially for a new site.

This section is filled with common and straightforward jobs. In particular, you'll need to ensure that you keep WordPress's core version updated. Installing any necessary plugins should also be a priority. Certain APIs will require plugins in order to work or act as a  bridge between your site.

A display showing an SSL certificate issued by Let's Encrypt, highlighting its current valid status and expiry date. There is a purple graphic of a certificate to accompany the information.
The SSL information is required for a Let's Encrypt certificate.

Also, this is the perfect moment to look into the limitations of your hosting provider when it comes to using APIs from third parties. This might take the form of rates limits or blocking ports. This knowledge will help you develop the capabilities you want efficiently.

How do you integrate APIs from third parties with WordPress on (2 techniques)

The typical method for integration with third-party API integration is to create a hardcode for the integration. It's not the only method to proceed, though.

We're now going to examine the two main approaches for integrating third-party APIs together with WordPress on . Let's start by using the manual method before looking for WordPress plugins.

1. Send HTTP requests to the API via direct code

If you are using WordPress sites, you should utilize the endpoints and routes within the REST API. They're not just secure, but they'll also offer the best integration with your site.

The APIs of TikTok are all excellent as hard coding the functionality follows an established procedure. We'll review of how you can do this.

Integration of the TikTok API with WordPress

The first step is to have a developer account with the platform. It's a simple sign-up procedure. At this point, you'll see the APIs that are available:

TikTok's developer web page, showcasing APIs and tools such as Login Kit and Share Kit. Each includes concise descriptions next to illustrated examples of how they integrate TikTok's features into other apps.
TikTok's developer microsite showing two of a number of open APIs.

Choose the appropriate API to meet your requirements and then take the time to look at the documentation for it. This is how we'll integrate the Display API in this example. The API allows you to display your profile information and associated videos.

Graphic representation of a mobile app interface for uploading photos or videos. There are options to add from media albums or directly from TikTok, featuring vibrant illustrations and interactive buttons.
It is the TikTok Display API.

The other requirement is another requirement is the TikTok Login Kit. It allows you to authenticate your desktop application with the social media platform. It isn't within the subject of this article but requires a authorization and registration process.

The Login Kit is also necessary to receive an authorization token for the particular profile that you wish to show:

curl --location --request POST 'https://open.tiktokapis.com/v2/oauth/token/' \
 --header 'Content-Type: application/x-www-form-urlencoded' \
 --header 'Cache-Control: no-cache' \
 --data-urlencode 'client_key=CLIENT_KEY' \
 --data-urlencode 'client_secret=CLIENT_SECRET' \
 --data-urlencode 'code=CODE' \
 --data-urlencode 'grant_type=authorization_code' \
 --data-urlencode 'redirect_uri=REDIRECT_URI'
 

By using this access token you are able to call the GET from the /v2/user/info/ endpoint. This will provide basic information regarding a particular user's identity:

curl -L -X GET 'https://open.tiktokapis.com/v2/user/info/?fields=open_id,union_id,avatar_url' \
 -H 'Authorization: Bearer act.example12345Example12345Example'
 

This should return a dedicated response from the API with all pertinent information in JSON format:


 "data":
 "user":
 "avatar_url":"https://p19-sign.tiktokcdn-us.com/tos-avt-0068-tx/b17f0e4b3a4f4a50993cf72cda8b88b8~c5_168x168.jpeg",
 "open_id":"723f24d7-e717-40f8-a2b6-cb8464cd23b4",
 "union_id":"c9c60f44-a68e-4f5d-84dd-ce22faeb0ba1"
 
 ,
 "error":
 "code":"ok",
 "message":"",
 "log_id":"20220829194722CBE87ED59D524E727021"
 
 
 

This response will need you to decode it in order to get the information you require and display it on the front-end. After you've created a functional implementation, you are able to examine, test and build error handling into the code. But, there's another way to set up APIs from third parties in WordPress and .

2. Make use of a WordPress plugin to provide an easy way for connecting to APIs from third parties

As is typical for WordPress the platform, there are plugins that allow you to connect to APIs from third parties efficiently and quickly way. In addition, you usually aren't required to be a developer to be able to comprehend and utilize them. You can enjoy all the advantages of hooking into APIs without the need for code.

There are a few plugins that could help in this regard, including Get the APIs you need. We prefer WPGetAPI but, in spite of that, it has a wide range of functions with its free version.

Header image for the WPGetAPI plugin's webpage, emphasizing the plugin's capability to facilitate connections to unlimited APIs. The design also highlights the plugin’s API integration features.
The WPGetAPI plugin header picture comes from WordPress.org.

Utilizing WPGetAPI in order to establish connections to APIs from third parties

This example will choose the ShortPixel Image Optimization API. Head to the WPGetAPI Setup screen. You'll be able to see an array of blank fields:

A detailed screenshot of the WPGetAPI plugin interface, showing input fields for the API's name, unique identification, and base URL for integration.
The primary WPGetAPI plugin screen.

The first step is to define three things: an API name, an unique ID and your base URL for the API.

The ShortPixel API configuration within the WPGetAPI plugin. It includes sections for entering API details such as name, unique ID, and the API's base URL.
Setting up ShortPixel's API information within WPGetAPI.

When you've filled these fields in and press the Save button, there will be a tab that allows you to select the endpoints. The initial four fields on this screen are essential to fill in. It is necessary to enter a unique ID and the URL of your endpoint, indicate a request method, and specify a format for the results:

An interface screenshot of WPGetAPI plugin showing the setup process for integrating ShortPixel API. There are fields for setting up endpoint details like Unique ID, method, and response format.
Setting up a POST call to ShortPixel's API in WPGetAPI.

When it comes to the request method drop-down menu, you'll often use only two choices - POST and GET for fetching data from the API, and POST for sending information to it.

ShortPixel works by first transmitting an image to it and then making use of an POST method. Most API requests will need an request method as well, so your site can take in data and send it.

There is a set of additional fields beneath the main four to set times for connection and cache. Then, based upon the method you use to authenticate the credentials you enter into the fields: Query String, Headers, or Body fields. ShortPixel uses an API key inside the URL string. Therefore, we're able to add our API key into the query string fields:

Details the ShortPixel API configuration endpoint within WPGetAPI. It shows fields for Unique ID, Method (POST), Timeout, Endpoint (/v2/reducer.php), Results Format (JSON string), and Cache Time. It includes a query string section with a key-value pair input.
Adding access key information to the Query String field in WPGetAPI.

Make sure to save your changes again and then go back to the upper right hand corner of the screen to see the Test Endpoints button. By clicking this, you can test the connection, then return an information report with connection status:

The WPGetAPI plugin displaying a successful test of the ShortPixel API endpoint 'squash_img'. It shows a green success message, the full URL called, and the data output section, indicating the API is functioning correctly and returning expected results.
Test an endpoint in WPGetAPI.

The final task for connecting the API to WordPress is to copy either the shortcode tag or template to the top of the page:

The ShortPixel API page in the WPGetAPI plugin, highlighting the template tag and shortcode for integrating the 'squash_img' endpoint. It provides fields for the base URL, endpoint ID, and a debug option set to false.
Choosing a method of displaying API details in WPGetAPI.

It is possible to paste them in a pertinent point on either your code or on your web page, based on which you like.

Analyzing the performance of third-party APIs by using APM

The Application Performance Monitoring (APM) section within the My dashboard, with an option to enable APM. The page outlines the purpose of APM in identifying sources of slow performance on websites and includes a caution about its potential impact on site performance while enabled.
The APM screen that is part of My Dashboard.

This lets you judge the quality of performance you can judge for your website through the My dashboard. It offers a useful list of metrics and charts that can help you evaluate the efficiency of your site:

The  APM report interface with a transaction sample including timestamp, transaction type (/wp-cron.php), URL, duration, and result (HTTP 200). Below, a transaction trace timeline graphically displays the duration and percentage of database operations such as wp_options SELECT and wp_terms SELECT.
The APM tool that reports on transactions times on a website.

It is important take a look at the effect of external HTTP process and request. If your API requires a lot of resources, then it's time to begin working in reworking it for better its efficiency. Being able to see these metrics at a close range of your dashboard will give you constant reminders of the areas your website needs to be improved.

Summary

Incorporating APIs from third party providers with WordPress on will help you extend your site's functionality. This can save you further development time, increasing UX, and increasing effectiveness. Even better, WordPress gives you a few ways to hook third-party APIs up.

Do you think third-party APIs should be considered for integration into WordPress? Please share your views in the comment section below!

Jeremy Holcombe

Content & Marketing Editor , WordPress Web Developer, and Content writer. In addition to all things WordPress I like the beach, golf, and watching movies. Additionally, I'm tall and have problems ;).