I'm using WP Rocket along with their helper plugin WP Rocket | Cache WP Rest API which allows caching of WordPress REST API endpoints.
I’ve added the following filter to allow REST API caching:
add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' );
Now it's caching API routes like:
/wp-json/wc/store/v1/cart
/wp-json/custom/v1/xyz
However, I want to exclude certain endpoints (for example, /wp-json/wp/v2/posts or /wp-json/custom/v1/sensitive-data) from being cached.