MantleClient

The backing library used by the <MantleProvider> is useful if you want to roll your own service or provider, or are using React class components instead of functional components and want to be able to easily query the Mantle API. You can install this package separately with npm install @heymantle/client.

/**
 * Initialize the Mantle client
 * @param {string} appId - Your Mantle App ID
 * @param {string} customerApiToken - The customer's API token
 * @param {string} apiUrl - The Mantle API URL, defaults to https://appapi.heymantle.com/v1
 * @returns {MantleClient}
 */
const mantleClient = new MantleClient({ appId, customerApiToken, apiUrl });

/**
 * Get the customer and its current subscription, available plans, etc
 * @returns {Promise<{ customer: Customer }>}
 */
async getCustomer();

/**
 * Send a usage event
 * @param {Object} params - The usage event options
 * @param {string} [params.eventId] - The ID of the event
 * @param {string} params.eventName - The name of the event which can be tracked by usage metrics
 * @param {string} params.customerId - Required if customerApiToken is not used for authentication. One of either the customer token, Mantle customer ID, platform ID / Shopify Shop ID, Shopify myshopify.com domain
 * @param {JSON} params.properties - The event properties
 * @returns {Promise<boolean>} true if the event was sent successfully
 */
async sendUsageEvent({ eventId, eventName, customerId, properties = {} }) {
  return await this.mantleRequest({
    path: "usage_events",
    method: "POST",
    body: {
      eventId,
      eventName,
      ...(customerId ? { customerId } : {}),
      properties,
    },
  });
}

/**
 * Initiate the Shopify subscription flow for the selected plan
 * @param {string} planId - The ID of the plan to subscribe to
 * @param {string} discountId - The ID of the discount to apply to the subscription
 * @param {string} returnUrl - The URL to redirect to after the subscription is complete
 * @returns {Promise<{ subscription: Subscription }>}
 */
async subscribe({ planId, discountId, returnUrl });

/**
 * Cancel the customer's current subscription
* @returns {Promise<{ subscription: Subscription }>}
 */
async cancelSubscription()