API Request Reference
Filter
rest_{endpoint}_request
This filter allows developers to modify REST API requests made to a specific SureCart endpoint before the request is processed. This is especially useful when you want to inject custom parameters, metadata, or handle authorization logic dynamically.
Usage
You can hook into this filter using add_filter() and provide your custom logic based on the REST method and endpoint.
Let us understand this by an example, Below is the filter for the checkouts endpoint.
rest_checkouts_request
This filter will allow us to modify the checkout requests made to the platform from the plugin.
add_filter(
'rest_checkouts_request', // you can replace checkouts with any other endpoint you want to filter.
function ( $request = null, $method = null ) { // $request is the WP_REST_Request object, $method is the method of the request.
if ( ! $request instanceof WP_REST_Request || 'edit' !== $method ) { // methods can be create, edit, delete & find.
return $request;
}
// Add your custom logic here.
// For example, you can modify the metadata param in the checkout request.
$request_metadata = $request->get_param( 'metadata' );
$metadata = (object) array_merge(
$request_metadata,
[
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]
);
$request->set_param( 'metadata', $metadata );
return $request;
},
10,
2
);
Parameters
- $request (WP_REST_Request|null)
The current REST request object. Use this to inspect or modify request data like headers, parameters, and body. - $method (string|null)
The REST action being performed. Possible values include:- create – when a new resource is being created.
- edit – when an existing resource is being updated.
- delete – when a resource is being removed.
- find – when data is being queried or fetched.
Notes
- This filter is dynamic. To apply it to other endpoints (e.g., products, subscriptions), simply replace checkouts in the filter name with the desired endpoint.
For example: rest_products_request / rest_subscriptions_request - Always validate the request and method before applying changes to avoid unexpected behavior.
Updated 6 days ago