Blog

🔌 Accessing REST API data from WordPress plugins

Leonardo Losoviz
By Leonardo Losoviz ·

Many WordPress plugins expose data via the REST API but don’t provide a GraphQL layer. With Gato GraphQL you can still use that data in a single GraphQL request: the HTTP Client extension lets you call any REST endpoint and work with the JSON response directly in your query.

So when a plugin has no GraphQL integration, you’re not stuck—you query their REST API from GraphQL and keep everything in one place.

This post shows how to do that. The same pattern works for any plugin that exposes REST endpoints.

Prerequisites

  1. Make sure the HTTP Client extension is installed (included in Gato GraphQL Power extensions and bundles).
  2. Configure the Allowed URLs so the plugin’s REST base is allowed. For same-site requests, allow your site URL (e.g. #https://yoursite.com/wp-json/.*# or your exact REST base). See configuring what URLs can be HTTP requested.

If the plugin’s API requires authorization, you’ll need to create the authorization token, and pass it in the request (e.g. via headers).

Example: Retrieving appointment booking data

BookingPress is an appointment booking plugin that offers REST API endpoints to retrieve appointment data. As such, we can call those endpoints from GraphQL and retrieve the appointment data.

Checking the BookingPress REST API documentation, we see that the current endpoint base is wp-json/bookingpress/v1.

1. List appointments (collection)

Use _sendJSONObjectCollectionHTTPRequest when the API returns a list of items (e.g. an array of appointments). If the API wraps the list in an object (e.g. { "data": [ ... ] }), you may need to use _sendJSONObjectItemHTTPRequest and then read the data property from the result.

Build the URL from your site’s home URL using optionValue(name: "home") so the same query works on any environment:

query GetBookingPressAppointments {
  siteURL: optionValue(name: "siteurl")
    @remove
 
  restBase: _sprintf(
    string: "%s/wp-json/bookingpress/v1/appointments",
    values: [$__siteURL]
  )
    @remove
 
  bookingpressApiKey: _env(name: "BOOKINGPRESS_API_KEY")
    @remove
 
  authorizationHeader: _sprintf(
    string: "x-bookingpress-api-key %s",
    values: [$__bookingpressApiKey]
  )
    @remove
 
  appointments: _sendJSONObjectCollectionHTTPRequest(
    input: {
      url: $__restBase,
      method: GET,
      options: {
        headers: [
          {
            name: "Authorization",
            value: $__authorizationHeader
          }
        ]
      }
    }
  )
}

Define BOOKINGPRESS_API_KEY in your environment (e.g. in wp-config.php). The query reads it via the PHP Constants and Environment Variables extension’s _env field, and then @removes it from the response.

// In wp-config.php
define( 'BOOKINGPRESS_API_KEY', 'your-secret-key' );

2. Single appointment by ID

For one appointment, use _sendJSONObjectItemHTTPRequest and build the URL with the ID:

query GetBookingPressAppointment($appointmentId: ID!) {
  siteURL: optionValue(name: "siteurl")
    @remove
 
  restURL: _sprintf(
    string: "%s/wp-json/bookingpress/v1/appointments/%s",
    values: [$__siteURL, $appointmentId]
  )
    @remove
 
  bookingpressApiKey: _env(name: "BOOKINGPRESS_API_KEY")
    @remove
 
  authorizationHeader: _sprintf(
    string: "x-bookingpress-api-key %s",
    values: [$__bookingpressApiKey]
  )
    @remove
 
  appointment: _sendJSONObjectItemHTTPRequest(
    input: {
      url: $__restURL,
      method: GET,
      options: {
        headers: [
          {
            name: "Authorization",
            value: $__authorizationHeader
          }
        ]
      }
    }
  )
}

3. Extracting data from the response

You can extract the specific properties from the response that you need, and use them in your query.

Use _underJSONObjectProperty to navigate to the property from the response object, and @export to extract the value and make it available in the query.

query GetBookingPressAppointment($appointmentId: ID!) {
  # ...
  appointment: _sendJSONObjectItemHTTPRequest(
    input: {
      url: $__restURL,
      method: GET,
      options: {
        headers: [
          {
            name: "Authorization",
            value: $__authorizationHeader
          }
        ]
      }
    }
  )
    @underJSONObjectProperty(by: { path: "data.id" })
      @export(as: "appointmentId")
    @underJSONObjectProperty(by: { path: "data.selected_date" })
      @export(as: "selectedDate")
    @underJSONObjectProperty(by: { path: "data.start_time" })
      @export(as: "startTime")
    @underJSONObjectProperty(by: { path: "data.service_id" })
      @export(as: "serviceId")
    @underJSONObjectProperty(by: { path: "data.customer_id" })
      @export(as: "customerId")
}
 
query DoSomethingWithTheAppointment @depends(on: "GetBookingPressAppointment") {
{
  # Do something with the appointment data
  appointmentId: _echo(value: $appointmentId)
  selectedDate: _echo(value: $selectedDate)
  startTime: _echo(value: $startTime)
  serviceId: _echo(value: $serviceId)
  customerId: _echo(value: $customerId)
}

Same pattern for other plugins

For any plugin that exposes REST endpoints:

  1. Find the base URL and path (e.g. from the plugin’s REST/API docs).
  2. Add that URL (or a regex for it) to the HTTP Client’s allowed list.
  3. If the API requires auth, use options.headers (or options.auth for basic auth) in the input to the _send* field.
  4. Use _sendJSONObjectItemHTTPRequest for a single resource and _sendJSONObjectCollectionHTTPRequest for a list.
  5. Extract the specific properties from the response that you need, and use them in your query.

You can combine these REST-backed fields with native GraphQL types (posts, users, etc.) in one query, so the client gets a single response mixing WordPress core data and plugin data from their REST API.

For more examples of calling REST and handling responses, see the HTTP Client extension docs and the tutorial on retrieving data from an external API.


Discover the power

Try demo now!

Play with Gato GraphQL + extensions in your own sandbox site, for free