The WPGetAPI Pro Plugin extends the free WPGetAPI plugin by adding various enhancements to help make working with external API’s even easier. The Pro plugin includes features such as caching, connecting your WordPress forms to your API, formatting options and more.
Features at a glance
- Cache API calls – choose the cache time per endpoint
- Dynamic variables – dynamic variables in endpoints, query string, headers and body
- Retrieve nested data – get data from nested, multidimensional arrays
- Base64 encoding – simple method to set a base64 encoded Authorization header
- Format as HTML – format your output as HTML using the shortcode
- Format as a number – format your output as a number using the shortcode
- XML format – as string or as array data
Cache API calls
Caching of API calls is extremely common and useful as it helps to avoid hitting API limits as well as helping the speed/performance of your website.
The speed of your website can be greatly affected if every time the page loads, you are also calling an API to fetch some data. Caching solves this issue by temporarily saving the API data in your own database and only calling the API again after your set interval. Put another way; if you have 100 people visit your page (within your cache interval time) then only the first visitor will trigger the call to the API and this first call will save the data locally on your own database. The subsequent visitors will then get this same data as the first call and avoid many other calls to the API.
Most API’s will have limits to the number of times their API can be called. Once you reach this limit, either the API will stop returning data for a set period of time or they may charge you extra $$ when you go over this limit. Caching can help easily avoid these limits by allowing you to select the interval and thus selecting the max number of times per day the API can be called. For example if you set the interval to 3600 (1 hour), you will only ever call this endpoint once every hour or 24 times a day. This makes it really simple to be able work within the limits of any API.
The Pro Plugin adds a field to each endpoint in the settings, allowing you to easily set the cache time on any endpoint.
View the docs on how to cache API calls.
Within WPGetAPI, you are able to manually set values within the Endpoint, Query String, Headers and Body POST Fields as shown below. These values are static values and don’t change (unless you manually change them yourself).
With dynamic variables we can now use variable values within the API call. To setup dynamic variables will require a small PHP code snippet to be added to your functions.php file (or via a code snippets plugin) so that the dynamic value can be captured (usually from a form).
Some examples of using dynamic variables could be:
- send product purchase data to your API that includes user details and purchased items
- a user fills in a form on your website and the form data is sent to your API
Dynamic variables can be used in the query string, endpoints, header or body.
Dynamic variables in the query string
The query string is the part of the URL that starts with a question mark and looks something like the bold text in this URL: https://thebaseurl.com/theendpoint/?foo=bar&dynamic_variable=345678
View the docs on how to use dynamic variables in query string.
Dynamic variables in endpoints
A variable endpoint will look something like this: https://thebaseurl.com/theendpoint/345678
View the docs on how to use dynamic variables in endpoints.
Dynamic variables in headers
A common example might be to send an ‘Authorisation’ header or a ‘content-type’.
View the docs on how to use dynamic variables in headers.
Dynamic variables in body
Variable data can be sent in the Body of your API call (sometimes called POST fields) and it can be sent as plain data, JSON encoded or URL encoded using the options in the endpoint settings. A common use for this is to send login details such as a username and password or to send captured form data to an API.
View the docs on how to use dynamic variables in body.
Retrieve nested data
Allows you to step down through multidimensional array data using array keys and indexes to get your data. This works in both the template function and also the shortcode.
View the docs on retrieving Nested Data.
A method to be able to easily set a base64 encoded Authorization header. After enabling the Pro Plugin, you can add your details within the headers like below.
Whatever you wrap in the base64_encode() function will be encoded.
Format data as HTML
The output of your API data can be formatted into HTML by using the ‘format’ attribute in the shortcode.
Firstly ensure that within the endpoint settings, you have the Results Format set to PHP array data and then set the ‘format’ attribute to html as shown below. This will wrap your entire output in a div and will then wrap individual API items in div tags as well.
[wpgetapi_endpoint api_id='quotable' endpoint_id='random' format='html']
For more info, we have an article within our docs on how to format API data into HTML.
Format number in shortcode
If the output of your API is a number, you can format it using the ‘format’ attribute in the shortcode.
Set the ‘format’ attribute to number_format(2) as shown below. This will format the number to have 2 decimal places. You can format from 0 up to any number you choose and will format based on the locale of your site.
[wpgetapi_endpoint api_id='binance' endpoint_id='price_of_coin' keys='price' format='number_format(2)']
This attribute uses the native WordPress function of number_format_i18n as seen here – https://developer.wordpress.org/reference/functions/number_format_i18n/
Adds the option to output the data in XML format, if your API supports that.
We are always happy to help with setup of our plugins – simply put in a support ticket and we will help you get started.