Using Variables

Variables let you inject dynamic values into URLs query parameters headers and bodies. You can pull data from the current site the active user the current request a post a WooCommerce order or a form submission. Variables use double square brackets like [[variable]].

Where to define variables

  • Create global variables under API Press → PRO → Variables for values shared across APIs
  • Create API specific variables inside the API tab if a value only applies to that connection

If a name exists in both places the API specific variable takes priority.

Supported variables

  • site[[site.url]] [[site.name]] [[site.admin]] [[site.language]] [[site.charset]] [[site.timezone]]
  • request[[request.uuid]] [[request.timestamp]] [[request.ts]]
  • current_user[[current_user.email]] [[current_user.display_name]] [[current_user.roles.0]] [[current_user.meta.favorite_color]]
  • user (when the event provides a user) → [[user.id]] [[user.email]] [[user.meta.custom_key]]
  • post[[post.id]] [[post.title]] [[post.permalink]] [[post.author.email]] [[post.meta._yoast_wpseo_metadesc]] [[post.field.post_title]]
  • wc (WooCommerce) → [[wc.order.id]] [[wc.order.total]] [[wc.order.currency]] [[wc.order.billing.email]]
  • form[[form.id]] [[form.title]] [[form.fields.1]] [[form.fields.email]] [[form.fields.1.first]]
  • date:FORMAT[[date:Y-m-d]] [[date:Y-m-d H:i:s]]
  • oauth2[[oauth2:token]] or [[oauth2:header]] and alias [[oauth_2_token]]

Default values

Provide a fallback with |default: inside the token.

[[user.email|default:nobody@example.com]]
[[post.title|default:Untitled]]
[[date:Y-m-d|default:1970-01-01]]

Examples in requests

  • URL path: https://api.example.com/v1/users/[[user.id]]
  • Query: ?ref=[[site.url]]&ts=[[request.ts]]
  • Header: Authorization: Bearer [[oauth2:token]]
  • JSON body: {"email":"[[form.fields.email]]","total":"[[wc.order.total]]"}

Notes

  • Use [[user.meta.KEY]] [[current_user.meta.KEY]] or [[post.meta.KEY]] to pull from meta
  • Dot notation walks through nested arrays and objects

Developer hooks

  • Register a custom resolver: register_resolver( 'myns', $callable )
  • Provide event context: register_context_provider( 'my_event', $callable )
  • Filters: apipress_pro_variable_delimiters apipress_pro_variable_resolvers apipress_pro_variable_context_{event} apipress_pro_variable_value

Related