shortcode – how to display dynamic data obtained from external sourced (API) within page content

I have a page that is passed a value as a query string parameter ( I need to use that value to fetch some additional data via a 3rd party API and then display the fetched data on my page. I know I could build this into a custom page template, but will be used in different places on different pages throughout the site.

My first inclination is to create a shortcode that gets the query string val from $_GET(), makes the request to the API endpoint to get the dynamic data, populates the data into an HTML template and then prints the entire chunk of HTML (template and data) on the page right where the shortcode was called.

I’m not positive this is the “right” (or most efficient or even the “WordPress”) way to do it, so I’m definitely open to other suggestions. But if I were to go that way, here’s roughly what I’m thinking for the shortcode.

add_shortcode('user_inf', 'bene_get_user_inf_from_XXX');
function bene_get_user_inf_from_XXX() {
    $uid = $_GET('uid');
    if(!$uid) return;

    $xxx = new $XXX(XXX_API_KEY);
    $data = json_decode($xxx->getUser($uid));

    $out = '<ul id="userDataList">';
    foreach($data as $k => $v) {
        $out .= "<li>$k is set to $v";
    $out .= '</ul>';
    return $out;

It would be nice to have that array of values to somehow be able to use in the page so that I wouldn’t be tied to the same HTML output each time, but that’s not super-important at this point.

I know things need to be escaped properly and whatnot… just proof of concept. Anyway, is this a feasible approach for displaying dynamic content? Are there other ways I should consider? I was also thinking of making an AJAX request to the API endpoint when the page loads and manipulating the DOM with the returned values, but this seems WAY more efficient to me. Thoughts?