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
Using Template Tag
- query_variables should be included within the array for the third argument of the template tag
- the value must be a string
- key/value pairs should follow this format key=value
- multiple key/value pairs should be separated with a comma as shown
// our made-up API key
$api_key = 'x0hj309jk2332d7fjkdjf9slsqwe';
// call the API using the wpgetapi_endpoint() template tag
$data = wpgetapi_endpoint( 'myapi', 'myendpoint',
array(
'debug' => true,
'query_variables' => 'api_key=' . $api_key . ',user_id=123',
)
);
// you now have the data stored within $data to use however you like
echo $data;
Using Shortcode
You can also use set query variables within the shortcode, though this will be less useful as you can’t really have a dynamic variable within a shortcode as such. But in this example below we are sending the api_key and the user_id as query variables.
[wpgetapi_endpoint api_id='myapi' endpoint_id='myendpoint' debug='true' query_variables='api_key=x0hj309jk2332d7fjkdjf9slsqwe,user_id=123']