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

@bunnyapp/api-client

v2.2.4

Published

Node.js client for Bunny CRM

Downloads

105

Readme

bunny-node

A node sdk for Bunny CRM

Setup

Install the latest package.

npm install @bunnyapp/api-client --save

Create a Bunny api client using either a valid access token or client credentials.

Access Token

The benefit of providing an accessToken is the request will be faster as an access token does not need to be generated. The downside of this approach is that if the token expires then your requests will start to fail.

const BunnyClient = require("@bunnyapp/api-client");
const bunny = new BunnyClient({
  baseUrl: "https://<subdomain>.bunny.com",
  accessToken: "<bunny-access-token>",
});

Client Credentials

Alternately you can provide clientId, clientSecret, & scope. In this case the client will generate an access token and if the token expires it will generate another one.

const BunnyClient = require("@bunnyapp/api-client");
const bunny = new BunnyClient({
  baseUrl: "https://<subdomain>.bunny.com",
  clientId: "<bunny-client-id>",
  clientSecret: "<bunny-client-secret>",
  scope: "standard:read standard:write",
});

Helper methods

This SDK wrappers several of the common Bunny API requests. The response varies based on the method that is used.

// Create a new subscription
const res = await bunny.subscriptionCreate("priceListCode", {
  trial: true,
  accountName: "accountName",
  firstName: "firstName",
  lastName: "lastName",
  email: "[email protected]",
  tenantCode: "remoteId",
});

// On success res will be a subscription object
{
  id: '17',
  account: { id: '18', name: 'accountName', contacts: [ [Object] ] },
  trialStartDate: '2023-07-17',
  trialEndDate: '2023-07-30',
  startDate: '2023-07-31',
  endDate: '2024-07-30',
  state: 'TRIAL',
  plan: { code: 'bunny_medium', name: 'Medium' },
  priceList: { code: 'bunny_medium_monthly', name: 'Monthly' },
  tenant: { id: '12', code: 'remoteId', name: 'accountName' }
}


// Get a session token for the Bunny customer portal
const res = await bunny.portalSessionCreate("tenantCode");

// Optionally supply a return url to get back to your app
const res = await bunny.portalSessionCreate(
  "tenantCode",
  "https://example.com"
);

// Default session length is 24 hours but you can change it. e.g 12 hours
const res = await bunny.portalSessionCreate(
  "tenantCode",
  "https://example.com",
  12
);

// Track usage for billing
const res = await bunny.featureUsageCreate(
  featureCode,
  quantity,
  subscriptionId,
  usageAt
);

// Update account details including billing address for acount
const res = await bunny.accountUpdateByTenantCode(
  "tenantCode",
  {
    billingStreet: "123 Main Street",
    billingCity: "Pleasantville",
    billingState: "CA",
    billingZip: "90210",
    billingCountry: "US"
  }
).then(console.log).catch(console.error)

Error handling

If there is an error with a helper method an exception will be raised.

try {
  const res = await bunny.subscriptionCreate(...);
  // Do something
} catch (error) {
  console.log(error);
}

or

bunny.subscriptionCreate(...).then((res) => {
  // Do something
}).catch((error) => {
  console.log(error);
})

Perform a query

If the convenience methods on this SDK are not enough and you need more control over queries or mutations then you can make an async request against the Bunny GraphQL API.

The response will contain the raw graphql json.

let query = `query tenants ($filter: String, $limit: Int) {
    tenants (filter: $filter, limit: $limit) {
        platform {
            id
            name
            code
        }
        id
        name
        code
    }
}`;

let variables = {
  filter: "",
  limit: 10,
};

let res = await bunny.query(query, variables);

Validate a webhook payload

When Bunny sends a webhook request it includes a x-bunny-signature header which can be used to validate the authenticity of the payload body.

Bunny will provide a signing token which you will need to store in your application and use for validating the webhook.

let signature = req.headers["x-bunny-signature"];
let payload = req.body;
let signingToken = "<secret signing token>";

let valid = bunny.webhooks.validate(signature, payload, signingToken);

Test

Run unit tests

npm test