HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Functions

This topic describes and provides and example of functions in the Optimizely Connect Platform (OCP).

Functions are small, executable blocks of code typically triggered off of a real-time event. An app can have any number of functions stored in the src/functions directory. Common examples of functions include:

  • Subscription to a List
  • Order Purchase/Refund/Cancellation
  • Marketing Consent UpdateSurvey Submission
  • Product Review/Rating

Functions can also be used to populate form fields as well.

The Basics

Functions that are defined in the app.yaml are provided a public URL in the following format.
https://function.zaius.app/<app_id>/<function>/<UUIDv4>

🚧

Important

Any publicly available functions (for example, webhooks) must be defined in the app.yml file.

A RESTful POST to this endpoint passes through the payload metadata to the function for processing however seen appropriate.

A live example may look like:
https://function.staging.zaius.app/my_app/cart/5f57ecbb-ef28-4a25-9056-3ea98994e2a5/

This is the BASE url of the function. As an app maker, you can support additional paths and query params added on to your url. For example:
https://function.staging.zaius.app/my_app/cart/5f57ecbb-ef28-4a25-9056-3ea98994e2a5/update?cart-id=12345

The path and query params are accessible to your function. For example:

import * as Zap from '@zaius/app-sdk';
import {z} from '@zaius/node-sdk';
export class Event extends Zap.Function {
  public async perform(): Promise<Zap.Response> {
    const path = this.request.path; // "/update"
    const cartId = this.request.params['cart-id']; // "12345"
    const body = this.request.bodyJSON || {}; // '{"request_id":"12345","products":[...], ...}'
  }
}

Example Function

import * as App from '@zaius/app-sdk';
import {logger} from '@zaius/app-sdk';
import {z} from '@zaius/node-sdk';
import {IncomingEvent} from '../data/IncomingEvents';
import {transformToCustomer} from '../lib/transformToCustomer';

/**
 * Example event handler.
 * Expects a request in the form:
 *  url: https://[webhook-url]/?email=<email>
 * with a JSON body.
 * Fires a Zaius event and updates the customer's name in Zaius
 */
export class HandleEvent extends App.Function {
  /**
   * Handle a request to the handle_event function URL
   * this.request contains the request information
   * @returns App.Response as the HTTP response
   */
  public async perform(): Promise<App.Response> {
    
    const email = this.request.params['email'] as string;

    if (!email) {
      return new App.Response(400, 'Missing required email parameter');
    } else {
      try {
        const event = this.request.bodyJSON as IncomingEvent;

        // TODO: transform your event data into zaius API calls
        if (event.customer) {
          await z.customer(transformToCustomer(event.customer));
        }
        await z.event({
          type: event.type,
          identifiers: {
            email
          },
          data: {
            action: event.action
          }
        });

        // return the appropriate status/response
        return new App.Response(200);
      } catch (e) {
        logger.error(e);
        return new App.Response(500, `An unexpected error occurred: ${e}`);
      }
    }
  }
}