Main site

Built-in Modules > Etsy Class

Etsy 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 Etsy class provides a mechanism to obtain real-time shipping rates for products available on Etsy.

Class Definition

class Etsy {
  constructor(destination, currency) {
    // Class constructor
  }

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

Arguments

  • destination (Object): This represents the "destination" information from the input DATA JSON object used in the calculateShippingRates function.
  • currency (String): The currency ISO code for which the shipping rates will be calculated.

Usage Example

const etsy = new Etsy(DATA.destination, DATA.currency);
const etsyProducts = [
  { etsy_product_id: "1383217118", etsy_shop_id: "630528612", quantity: 1 },
  // ... other products
];
const rates = await etsy.getRates(etsyProducts);

In this example, an instance of the Etsy class is created using destination details and the desired currency code. The getRates method then calculates shipping rates for an array of Etsy products, each defined by an etsy_product_id, etsy_shop_id, and quantity. The format of the returned rates is consistent with the format used by Shopify for shipping rates.

The etsy_product_id is the pathname after listing on the Etsy product page url. For example, https://www.etsy.com/au/listing/1533974422/wall-decor-wooden-world-map-enjoy-the, the product id is 1533974422.

The etsy_shop_id can be accessed by heading to Esty shop's front page, right clicking on "Contact" button, choose "copy link address", and paste it on a blank text editor, the number after recipient_id= is the shop id.

If the input currency is not supported by Etsy, the plug-in returns rates in USD currency.

Sample AI Prompt

I'd like a shipping rate calculator for items from Etsy.
Enrich the data with metafield namespace "market" with size 10.
The Etsy product id and shop id for each items is saved in the items object metafield "market" with key named "etsy_product_id" and "etsy_shop_id".

Sample AI Output

import { enrichItemDetails } from "./modules.js";
import { Etsy } 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 Etsy products from the items metafield "market".
    const etsyProducts = DATA.items.map((item) => {
      const marketMetafield = item.metafields.market;
      return {
        etsy_product_id: marketMetafield.etsy_product_id,
        etsy_shop_id: marketMetafield.etsy_shop_id,
        quantity: item.quantity,
      };
    });

    // Create an Etsy instance with the destination and currency.
    const etsy = new Etsy(DATA.destination, DATA.currency);

    // Get the shipping rates from Etsy.
    const rates = await etsy.getRates(etsyProducts);

    // 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: "",
        },
      ],
    };
  }
}