REST API

REST API Reference

4 min read

Notifal registers REST routes under the namespace notifal/v1. Base URL:

/wp-json/notifal/v1/

Unless noted, public frontend routes use permission_callback => __return_true and rely on server-side eligibility checks rather than WordPress login.

Implementation: ApiRegistrar.php (on-page), TagsApiRegistrar.php, DynamicKeysApiRegistrar.php.

On-page notifications

GET /onpage/eligible

Returns notifications eligible for the current visitor context.

Query parameters (context):

ParameterTypeDescription
page_idintCurrent post ID
urlstringCurrent page URL
user_idintLogged-in user ID
device_typestringdesktop, mobile, or tablet
post_typestringSingular/archive context post type
archive_taxonomystringTaxonomy on archive views
categoriesstringComma-separated category IDs
product_categoriesstringWooCommerce product category IDs
user_rolesstringComma-separated role slugs
is_front_page, is_shop_page, is_cart_page, etc.stringBoolean flags from frontend context
refresh_notification_idintRe-fetch one notification (retrigger)
client_seen_sourcesstringJSON for content-source rotation
cache_buststringCache bypass token

Filters: notifal/onpage/eligibility/data, actions notifal/onpage/eligibility/before_process / after_process.

Used by: frontend notification-manager.js (apiEndpoint in localized config).

GET /onpage/preview

Admin preview of a single notification (same shape as eligible items).

ParameterRequiredDescription
idyesNotification post ID
_preview_tokennoPreview security token
device_typenodesktop, mobile, tablet

POST /onpage/track

Records impression, click, close, or dismiss events.

Body fieldRequiredDescription
notification_idyesNotification post ID
event_typeyesimpression, click, close, dismiss
timestampnoUnix timestamp (default: now)
user_agentnoClient user agent
referrernoReferrer URL
page_urlnoPage where event occurred

Filters: notifal/onpage/tracking/data, notifal/onpage/tracking/use_queue, notifal/onpage/tracking/validation/rules.

Actions: notifal/onpage/tracking/before_process, notifal/onpage/tracking/event_stored.

GET /onpage/preferences

Returns stored visitor notification preferences (consent / frequency).

POST /onpage/preferences

Updates preferences.

Body fieldTypeDescription
preferences.enabledboolMaster preference toggle
preferences.frequencystringlow, medium, high
preferences.categoriesstring[]Opt-in categories

GET /onpage/cart-context

WooCommerce cart snapshot for client-side cart display rules (when WooCommerce is active).

Returns normalized cart data used by frontend rule evaluation. Filter: notifal/onpage/woocommerce/cart_context.

Tags and dynamic keys

GET /tags

Returns all registered tags grouped by category for builders and admin pickers.

Filter: notifal/tag/api/modify_response.

Permission: public read.

GET /dynamic-keys

Search/list meta keys for dynamic tag configuration in admin.

Permission: edit_posts.

Filters: notifal/dynamic_keys/most_used, notifal/dynamic_keys/search_results.

Templates (additional routes)

RouteMethodPurpose
/templates/previewGETTemplate preview iframe/API
/featured-image-previewGETFeatured image preview helper

Registered under app/Modules/Templates/Infrastructure/WordPress/Registration/.

Rate limiting and security

  • Eligibility and track endpoints support configurable rate limits via notifal/onpage/api/rate_limit_settings.
  • Always validate and sanitize data in filters; do not expose privileged meta in API responses.
  • Admin-only routes should keep strict permission_callback checks (see dynamic-keys).

Example: read eligible notifications in PHP

add_action( 'rest_api_init', function () {
    // Prefer hooks over duplicating eligibility logic.
} );

add_filter( 'notifal/onpage/eligibility/data', function ( $data, $context ) {
    // $data: notifications array prepared for frontend.
    // $context: resolved page context.
    return $data;
}, 10, 2 );

For changing a single notification payload, use notifal/onpage/notification/content instead (see cookbook).

  1. REST API Reference (you are here)
  2. Hooks: Notifications and Eligibility
  3. Hooks: Analytics and Tracking
  4. Cookbook: Modify Notification Before Display