What are Dynamic Variables?
When you setup an endpoint using WPGetAPI, the settings that you add to the endpoint settings page within the admin area are static values. Meaning that you set the value and that value remains the same. To change that value, you need to manually edit it and then re-save the endpoint.
Dynamic Variables allow you to set dynamic values that can be sent to your API. The dynamic value could come from form inputs, data within your WordPress site such as user data, or any other type of dynamic value can think of.
Where do Dynamic Variables work?
Dynamic Variables work best when used in conjunction with the Template Tag, but they also work within the Shortcode although this option is a little less flexible due to the way shortcodes work.
Dynamic Variables can be used with:
Using Dynamic Variables in the Endpoint
In our example we will use a real life API to retrieve a random cat image: https://thatcopy.pw/catapi/docs.html
We are wanting to call this endpoint in particular: https://thatcopy.pw/catapi/restId/1
Notice the number 1 at the end of the endpoint. This number represents the ID of the cat picture that we will retrieve and we want to make it a random number, which will display a different cat pic depending on the number.
Endpoint settings
Within the admin setup of your endpoint, we would replace the number 1 from our example above with the number 0 wrapped in curly braces – {0} – so our endpoint now looks like the image below.
Wrapping the number in {} firstly tells the plugin that this is a variable in the endpoint. Using the number 0 means it is our first variable as it is ‘zero indexed’. If there were multiple different variables required, they would just be sequentially numbered like {0} {1} {2} etc. You can use unlimited variables.
Let’s now look at what we need to add to the template tag and the shortcode to get this working.
Using with the Template Tag
We are just looking to get a random number for this, so we can simply use the built in PHP function rand() to generate a random number for us. The code looks like this:
Using Dynamic Variables in the Query String
Adding dynamic variables into the query string is possible using the query_variables parameter that you can use in the template tag or the shortcode.
The query string is the part of a URL after the question mark – it is highlighted in bold in this example below:
https://someapiurl.com/endpoint/?api_key=1234567&user_id=456
Let’s now look at how we implement dynamic variables within the shortcode and within the template tag.
Using Dynamic Variables in the Headers
Adding dynamic variables into the headers that are sent to your API is possible using the headers_variables parameter. The headers_variables parameter is available in the template tag only, not the shortcode.
Using with the Template Tag
Here is how we could dynamically encode some data to send this as an Authorization header.
Using Dynamic Variables in the BODY Post Fields
Sending dynamic variables in the body is possible using the body_variables parameter that you can use with the template tag or the shortcode.
The Request Method within the endpoint settings must be set as either POST, PUT or DELET, depending on your API.
Using with the Template Tag
Here is how we would dynamically send data into the body – sometimes called post fields. This example simply uses a username and password array.