Main site

Built-in Modules > Spocket Class

Spocket 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 Spocket class is designed to interact with the Spocket platform to obtain real-time shipping rates for Spocket-listed products using the user's account credentials.

Class Definition

class Spocket {
  constructor(spocket_email, spocket_password, destination) {
    // Class constructor
  }

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

Arguments

  • spocket_email (String): The email address associated with the your Spocket account.
  • spocket_password (String): The password for the your Spocket account.
  • destination (Object): The "destination" field from the input DATA JSON object that is passed to the calculateShippingRates function.

Usage Example

import { Spocket } from "./modules.js"
const spocket = new Spocket(env.spocket_email, env.spocket_password, DATA.destination);
const spocketProducts = [
  { spocket_product_id: "61c14fc3-9e49-47a9-bf01-da4409836788", quantity: 1 },
  { spocket_product_id: "5afa6523-7687-4724-a0ce-36c8b635ba17", quantity: 2 },
  // ... more products
];
const rates = await spocket.getRates(spocketProducts);

In this usage example, the Spocket class is instantiated with account credentials and the destination information. The getRates method fetches shipping rates for a collection of Spocket products, each identified by a spocket_product_id and its quantity. The format of the returned rates is compatible with Shopify's rates structure. For better security, your Spocket credentials should be saved in the env object as encrypted secret variables in the JsRates Settings page.

The spocket_product_id is the last pathname of the Spocket product page url. For example, https://app.spocket.co/product/2e9709c7-c95b-4d05-b55b-ba34a32acf87, the product id is 2e9709c7-c95b-4d05-b55b-ba34a32acf87.

The Spocket plug-in returns rates in USD currency. Use our built-in currency converter class Currency to convert to other currencies.

Sample AI Prompt

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

Sample AI Output

import { enrichItemDetails } from "./modules.js";
import {Spocket} 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 Spocket products from the metafield
    const spocketProducts = DATA.items.map((item) => ({
      spocket_product_id: item.metafields.market.spocket_product_id,
      quantity: item.quantity,
    }));
    // Create a new Spocket instance
    const spocket = new Spocket(env.spocket_email, env.spocket_password, DATA.destination);
    // Get the shipping rates from Spocket
    const rates = await spocket.getRates(spocketProducts);
    // Return the shipping rates in the Shopify format
    return { rates };
  } catch (error) {
    // Return an error rate
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}