Before you do anything
We recommend that you have at least a basic understanding of how a REST API works and some of the terminology. If you are unsure, please have a quick read of this article on understanding and using REST API’s as well as reading through the documentation of the API that you are wanting to connect to.
Setting up your first API
After installing and activating the plugin, click on the WPGetAPI link in the sidebar of your WordPress admin which will take you to the Setup screen. Here you can add your first API.
In the screenshot above, we have added the API details for the Quotable API which is an API that will simply return some random quotes.
The fields that you will need to fill in are:
- API Name – the name of your API and you can really call it whatever you like. Whatever you put in this field will become the name of the tab and the menu item as shown in the screenshot – here they are both called ‘Quotable’
- API ID – a unique ID of your choosing. The API ID will be used in the shortcode or template tag when you want to call your API
- Base URL – the base URL of the API you are connecting to
Once you have filled in all 3 fields, you can click the Save button at the bottom of the page and your new tab called Quotable will appear. Now click on the tab to be taken to the next screen to set up your API endpoints.
Setting up your API endpoints
When setting up your API endpoints, the first fields you will see are:
- Timeout – this is how long (in seconds) you are willing to wait to connect to the API and return your data. Leaving this blank will set it at 10 seconds
- SSL Verify – this should always be set to True. Occasionally you may have issues connecting (usually on a local testing environment) and you can try setting this to False when testing. Please read about why it is a bad idea to set this to False.
Adding an endpoint
In the documentation for your API you should see information about all of the endpoints that your API can connect to.
Lets look at the fields for connecting to your chosen endpoints:
- Unique ID – choose a unique ID for your endpoint. This unique ID will be used in the shortcode or template tag when calling the endpoint
- Endpoint – this is the actual endpoint section of the URL that will be appended to the Base URL
- Method – the request method for your endpoint. Can be either GET and POST
- Results Format – the format of your API endpoint. You can return the data as either JSON (as string) which will return a string formatted JSON result or as PHP array data which will return the data as a PHP array that can then be manipulated further (if you know basic PHP). The JSON (as string) option should be used on the shortcode and you can use either option for the template tag
- Query String – add name/value parameters that will then be appended onto the URL. So adding something like api_key into the Name field and then 4d5r8s2c8e89d into the Value field would make the URL look like this: https://yourapi.com/yourendpoint?api_key=4d5r8s2c8e89d
- Headers – this is the same as the query string parameters, but instead of appending to the URL, your parameters are added to the headers. So if your API required, for example, your api_key to be sent in the headers you would add api_key into the Name field and your key into the Value field
- Body – again this is similar to the query string and header parameters but will add parameters into the Body of the request. This will only work for the POST request method
Once you have added all of your data for the endpoint you can hit the Save button and you are then ready to output the data using one of the methods below.
Using the template tag to output data
The template tag is by far the most flexible option as it allows you to get the results of the API call and put them into a variable that can then be used or manipulated however you choose. Some knowledge of PHP will be required for this.
Please see the guide on using the template tag.
Using the shortcode to output data
You can add the shortcode into your pages and posts to output API data. This option is more limited than using the template tag.
Please see the guide on using the shortcode.