npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@heymantle/client

v1.1.35

Published

Mantle API Client for JavaScript

Downloads

3,921

Readme

Mantle API Client for JavaScript

A simple interface for interacting with the Mantle App API.

Installation

You can install the Mantle App API library using npm:

$ npm install @heymantle/client

Usage

The first thing you'll want to do is identify the customer/shop to Mantle using the identify endpoint:

const { MantleClient } = require('@heymantle/client');
const client = new MantleClient({
  appId: process.env.MANTLE_APP_ID,
  apiKey: process.env.MANTLE_API_KEY,
});
const { apiToken: customerApiToken } = await client.identify({
  platform: 'shopify',
  platformId: shop.id,
  myshopifyDomain: shop.myshopifyDomain,
  accessToken: shop.accessToken,
  name: shop.name,
  email: shop.email,
  customFields: {
    'Custom 1': 1234,
    'Custom 2': 'abc',
  },
});

Once identified, you can perform customer-specific operations:

  • Fetch the customer, subscription, and plan details
  • Create and update the customer subscription
  • Push usage events associated with the customer

NOTE: Authentication

The primary way to authenticate is using the Mantle app ID and API key that you generate in the Mantle dashboard for a specific app. The API key should never be used in frontend code or code that will be exposed in the browser. Instead, you can use the customer API token returned from the identify request in frontend requests, but this is only secure when using it on frontend requests that are authenticated to that customer since it's exposing only their own token.

const client = require('@heymantle/client');
const client = new MantleClient({
  appId: process.env.MANTLE_APP_ID,
  customerApiToken: shop.mantleApiToken,
});

getCustomer()

Fetches the current/authenticated customer. For example:

const { customer } = await client.getCustomer();
console.log(customer)
{
  "id": "123",
  "test": false,
  "installedAt": "2023-10-25",
  "trialStartsAt": "2023-10-25",
  "trialExpiresAt": "2023-11-03",
  plans: {
    "id: "345",
    "name": "Advanced",
    ...
  },
  subscription: {
    ...
  },
  features: {
    ...
  },
  usage: {
    ...
  },
  customFields: {
    ...
  }
}

subscribe

Inititates the subscribe process, creates a pending subscription in Mantle and returns the subscription object, including the confirmationUrl that you would redirect the customer to in order to authorize the charge. For example:

const { confirmationUrl } = await client.subscribe({
  planId: "345",
  returnUrl: "https://myapp.com/charge_callback"
});

cancelSubscription

Cancels the current active subscription for the customer and returns the cancelled subscription object.

await client.cancelSubscription();

sendUsageEvent

Pushes a usage event to Mantle for this customer. Usage events can be used to send usage based billing events, such as when an order is created in Shopify, and Mantle will automatically create the appropriate usage charges for the customer. Usage events can also be used as a general purpose event stream where you can view reports and analytics on those events, including funnel events, for example when certain onboarding steps are completed.

await client.sendUsageEvent({
  eventId: 123, // optional idempotency key, if this isn't sent then Mantle will automatically generate the value
  eventName: "order_created",
  customerId: "example.myshopify.com", // this is only needed if you don't use customerApiToken authentication
  properties: {
    order_value: 150,
    processed_at: "2023-10-26",
    customer_name: "Peter Parker",
  },
});
await client.sendUsageEvent({
  eventName: "page_view",
  properties: {
    path: location.href,
  },
});

Documentation

Documentation is available at https://heymantle.com/docs/integrate.