Main site

Built-in Modules > Hypersku Class

Hypersku Class

Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.

Overview

The Hypersku class facilitates the retrieval of real-time shipping rates for products from Hypersku, incorporating user account credentials.

Class Definition

class Hypersku {
  constructor(hypersku_email, hypersku_password, destination, currency) {
    // Class constructor
  }

  async getRates(hyperskuProducts) {
    // Method to fetch rates
  }
}

Arguments

  • hypersku_email (String): The email associated with your Hypersku account.
  • hypersku_password (String): The password for your Hypersku account.
  • destination (Object): The "destination" field from the input DATA JSON object used in the calculateShippingRates function.
  • currency (String): The desired currency ISO code for the shipping rates.

Usage Example

import { Hypersku } from "./modules.js"
const hypersku = new Hypersku(env.hypersku_email, env.hypersku_password, DATA.destination, DATA.currency);
const hyperskuProducts = [
  { hypersku_product_id: "200405913600032", quantity: 1 },
  { hypersku_product_id: "700408995200015", quantity: 2 },
  // ... additional products
];
const rates = await hypersku.getRates(hyperskuProducts);

This example demonstrates initializing the Hypersku class with user credentials, destination, and currency. It then fetches shipping rates for an array of Hypersku products, where each product is specified by a hypersku_product_id and quantity. The resulting rates are formatted similarly to Shopify's shipping rates. For better security, your Hypersku credentials should be saved in the env object as encrypted secret variables in the JsRates Settings page.

The hypersku_product_id is the last pathname of the Hypersku product page url. For example, https://app.hypersku.com/products/detail/300412804200012, the product id is 300412804200012.

Ensure the currency you input is supported by Hypersku, as unsupported currencies will throw error. Additionally, initializing the Hypersku class will log you out of any active Hypersku admin sessions.

Sample AI Prompt

I'd like a shipping rate calculator for items from Hypersku products.
Enrich the data with metafield namespace "market" with size 10.
The Hypersku product id for each items is saved in the data items metafield "market" with key named "hypersku_product_id".
The Hypersku email and password are saved in the environment variable object.

Sample AI Output

import { enrichItemDetails } from "./modules.js";
import { Hypersku } from "./modules.js";

export async function calculateShippingRates(DATA, env) {
  try {
    // Enrich the data with metafield namespace "market" with size 10
    DATA = await enrichItemDetails(DATA, [{ namespace: "market", size: 10 }]);

    // Get the Hypersku email and password from the environment variable object
    const hypersku_email = env.hypersku_email;
    const hypersku_password = env.hypersku_password;

    // Create a Hypersku instance
    const hypersku = new Hypersku(
      hypersku_email,
      hypersku_password,
      DATA.destination,
      DATA.currency
    );

    // Get the Hypersku product ids from the data items metafield "market" with key named "hypersku_product_id"
    const hyperskuProducts = DATA.items.map((item) => {
      const hypersku_product_id = item.metafields.market.hypersku_product_id;
      return { hypersku_product_id, quantity: item.quantity };
    });

    // Get the shipping rates from Hypersku
    const rates = await hypersku.getRates(hyperskuProducts);

    // Return the shipping rates in the Shopify format
    return { rates };
  } catch (error) {
    // Return a rate with price equals to null, and other rate fields set to empty string
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}