January 2, 2025
O. Wolfson
When building an e-commerce platform that integrates with Printful for product fulfillment, accessing the Printful API becomes a crucial step. In this article, we'll dissect two scripts designed to interact with the Printful API. These scripts demonstrate how to retrieve product information from a store and fetch specific product variant details.
To follow along, ensure you have the following:
dotenv for environment variable management.This script retrieves a list of products available in the Printful store.
javascriptimport dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
const API_KEY = process.env.PRINTFUL_API_KEY;
console.log(API_KEY);
const fetchStoreInfo = async () => {
const response = await fetch("https://api.printful.com/store/products", {
headers: {
Authorization: `Bearer ${API_KEY}`,
},
});
const data = await response.json();
console.log(data);
};
fetchStoreInfo();
dotenv package is used to load the PRINTFUL_API_KEY from a .env.local file.Authorization header as a Bearer token.fetch function makes a GET request to https://api.printful.com/store/products.When executed, this script logs the store's product information to the console. This includes details like product IDs, names, and categories.
This script focuses on retrieving variant details for a specific product ID.
javascriptimport axios from "axios";
import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
const API_KEY = process.env.PRINTFUL_API_KEY;
const fetchProductDetails = async (productId) => {
try {
const response = await axios.get(
`https://api.printful.com/store/products/${productId}`,
{
headers: {
Authorization: `Bearer ${API_KEY}`,
},
}
);
return response.data.result.sync_variants || [];
} catch (error) {
console.error(`Error fetching product details for ID: ${productId}`, error);
throw new Error("Failed to fetch product details");
}
};
(async () => {
const productVariants = await fetchProductDetails(370972441); // Example product ID
console.log("Product Variants:", productVariants);
})();
fetch.fetchProductDetails function accepts a productId parameter, allowing dynamic API calls for various products.try-catch block ensures graceful error handling and logs any issues that arise during the API request.sync_variants array, which contains detailed variant information for the specified product.By providing the product ID 370972441 (a T-shirt, for example), the script logs an array of product variants, including size, color, and other attributes.
| Feature | Script 1 | Script 2 |
|---|---|---|
| HTTP Client | fetch | axios |
| Purpose | Fetches all store products | Fetches specific product info |
| Error Handling | Minimal | Comprehensive |
These scripts showcase the foundational steps for integrating the Printful API into an e-commerce platform. By retrieving both general and specific product information, you can power various features, such as product listings and detail pages. As you scale, consider optimizing these scripts for production environments, including caching and rate-limiting strategies.
| Static endpoint |
Dynamic endpoint via productId |