-
I want to list items to my seller hubs drafts, although when I run the code and it successfully executes, I do not see any new draft in my sellers hub, although when I fetch the new draft it says it exists. What could be the issue here? Code below:
// tslint:disable:no-console
import eBayApi from "ebay-api";
const eBay = new eBayApi({
appId: process.env.EBAY_APP_ID,
certId: process.env.EBAY_CERT_ID,
sandbox: false,
siteId: eBayApi.SiteId.EBAY_US,
marketplaceId: eBayApi.MarketplaceId.EBAY_US,
});
const token = process.env.EBAY_TOKEN_PROD
eBay.OAuth2.setCredentials(token);
async function createInventoryItem(sku: string) {
try {
const response = await eBay.sell.inventory.createOrReplaceInventoryItem(sku, {
condition: "NEW",
product: {
title: "Sample eBay Listing",
description: "This is a sample listing created via eBay API",
brand: "Generic",
imageUrls: ["https://example.com/sample.jpg"],
},
availability: {
shipToLocationAvailability: {
quantity: 10, // Required for listing the item
},
},
});
console.log("Full API Response:", JSON.stringify(response, null, 2));
} catch (error) {
console.error("Error creating inventory item:", error);
}
}
createInventoryItem("SKU12345"); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I have made progress and am now stuck on this error and unsure how to resolve no matter how many times I go around in the eBay docs or the OpenAPI JSON.
Current code: import eBayApi from "ebay-api";
import dotenv from "dotenv";
dotenv.config();
const eBay = new eBayApi({
appId: process.env.EBAY_APP_ID,
certId: process.env.EBAY_CERT_ID,
sandbox: false,
siteId: eBayApi.SiteId.EBAY_US,
marketplaceId: eBayApi.MarketplaceId.EBAY_US,
});
const token = process.env.EBAY_TOKEN_PROD;
eBay.OAuth2.setCredentials(token);
const sku = "test-01";
const countryCode = "US";
const merchantLocationKey =
process.env.EBAY_MERCHANT_LOCATION_KEYz || "default"; // ✅ Ensure location
// 🏢 Step 1: Check if Inventory Location Exists
async function checkInventoryLocation() {
try {
const locations = await eBay.sell.inventory.getInventoryLocations();
if (locations.locations.length > 0) {
console.log(
"✅ Inventory Location Found:",
locations.locations[0].merchantLocationKey
);
return locations.locations[0].merchantLocationKey;
} else {
console.log("❌ No Inventory Location Found. Creating one...");
return await createInventoryLocation();
}
} catch (error) {
console.error("❌ Error fetching inventory locations:", error);
return await createInventoryLocation();
}
}
// 🏬 Step 2: Create Inventory Location if None Exists
async function createInventoryLocation() {
const locationData = {
location: {
address: {
addressLine1: "123 Main St",
city: "New York",
stateOrProvince: "NY",
country: countryCode,
postalCode: "10001",
},
},
locationTypes: ["WAREHOUSE"],
merchantLocationStatus: "ENABLED",
name: "My eBay Store",
phone: "123-456-7890",
};
try {
await eBay.sell.inventory.createInventoryLocation(
merchantLocationKey,
locationData
);
console.log("✅ Inventory Location Created:", merchantLocationKey);
return merchantLocationKey;
} catch (error) {
console.error("❌ Error creating inventory location:", error);
throw error;
}
}
// 🛒 Step 3: Create or Update Offer with `merchantLocationKey`
async function createOrUpdateOffer() {
const locationKey = await checkInventoryLocation();
const offerData = {
sku,
marketplaceId: eBayApi.MarketplaceId.EBAY_US,
format: "FIXED_PRICE",
listingDuration: "GTC",
availableQuantity: 10,
categoryId: "12345",
pricingSummary: {
price: { currency: "USD", value: 99.99 },
},
listingPolicies: {
fulfillmentPolicyId: process.env.EBAY_FULFILLMENT_POLICY_ID,
paymentPolicyId: process.env.EBAY_PAYMENT_POLICY_ID,
returnPolicyId: process.env.EBAY_RETURN_POLICY_ID,
},
merchantLocationKey: locationKey,
location: { country: "US" },
product: {
title: "Casual White T-Shirt",
description: "A comfortable white t-shirt for everyday wear.",
// ✅ Ensure Brand and MPN are inside `aspects`
aspects: {
Brand: ["Casual"], // ✅ Ensure correct casing
MPN: ["Does Not Apply"], // ✅ Required if no real MPN
Size: ["S"],
"Size Type": ["Regular"],
Type: ["T-Shirt"],
Department: ["Men"],
Color: ["White"],
},
imageUrls: [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
],
},
};
try {
const existingOffer = await eBay.sell.inventory.getOffers({ sku });
if (existingOffer.offers.length > 0) {
console.log("♻️ Updating existing offer...");
await eBay.sell.inventory.updateOffer(
existingOffer.offers[0].offerId,
offerData
);
console.log("✅ Offer Updated:", existingOffer.offers[0].offerId);
return existingOffer.offers[0].offerId;
} else {
console.log("➕ Creating new offer...");
const response = await eBay.sell.inventory.createOffer(offerData);
console.log("✅ Offer Created:", response.offerId);
return response.offerId;
}
} catch (error) {
console.error("❌ Error creating/updating offer:", error);
throw error;
}
}
// 🚀 Step 4: Publish Offer
async function publishOffer(offerId) {
try {
console.log(`🚀 Publishing Offer ID: ${offerId}`);
const response = await eBay.sell.inventory.publishOffer(offerId);
if (response.listingId) {
console.log("✅ Offer Published! eBay Listing ID:", response.listingId);
} else {
console.error("⚠️ Offer published but no listing ID returned.");
}
} catch (error) {
console.error("❌ Error publishing offer:", error);
throw error;
}
}
// ✅ Run the full process
async function listItemOnEbay() {
try {
const offerId = await createOrUpdateOffer();
await publishOffer(offerId);
console.log("🎉 Item successfully listed on eBay!");
} catch (error) {
console.error("🚨 Failed to list item:", error);
}
}
// Start the listing process
listItemOnEbay(); |
Beta Was this translation helpful? Give feedback.
-
ResolvedI found this doc of eBay developers program, followed it and successfully listed an item via eBay's REST API.
import eBayApi from "ebay-api";
import dotenv from "dotenv";
dotenv.config();
// Initialize eBay API client with credentials
const ebay = new eBayApi({
appId: process.env.EBAY_APP_ID,
certId: process.env.EBAY_CERT_ID,
sandbox: false, // Set to true for testing in eBay's sandbox environment
marketplaceId: eBayApi.MarketplaceId.EBAY_US,
});
// Set OAuth token for authentication
const token = process.env.EBAY_TOKEN_PROD;
ebay.OAuth2.setCredentials(token);
// Define inventory item details
const sku = "test-01";
const merchantLocationKey = process.env.EBAY_MERCHANT_LOCATION_KEY || "default";
const categoryId = "262982"; // eBay category ID for the item
// Create or update an inventory item
async function createOrReplaceInventoryItem() {
const inventoryItemData = {
condition: "NEW",
conditionDescription: "Brand new item, never used.",
product: {
title: "Front door Rug",
description: "A beautiful modern rug for your home or office.",
brand: "Generic",
mpn: "12345",
imageUrls: ["https://imageur.com/5555.jpg"],
aspects: {
Brand: ["Generic"],
MPN: ["12345"],
Material: ["100% Polypropylene"],
Type: ["Area Rug"],
Color: ["Natural"],
Size: ["7' 6\" x 9' 6\""],
},
categoryId,
},
availability: {
shipToLocationAvailability: { quantity: 1 },
},
packageWeightAndSize: {
dimensions: { height: 5, length: 10, width: 7, unit: "INCH" },
weight: { value: 5, unit: "POUND" },
},
};
try {
await ebay.sell.inventory.createOrReplaceInventoryItem(
sku,
inventoryItemData
);
console.log("✅ Inventory Item Created/Updated");
} catch (error) {
console.error("❌ Error creating/updating inventory item:", error);
}
}
// Check if an offer already exists for the SKU
async function getOfferBySKU(): Promise<string | null> {
try {
const response = await ebay.sell.inventory.getOffers({ sku });
if (response.offers && response.offers.length > 0) {
console.log("⚠️ Offer already exists:", response.offers[0].offerId);
return response.offers[0].offerId;
}
return null;
} catch (error) {
console.error("❌ Error checking existing offer:", error);
return null;
}
}
// Create a new offer or update an existing one
async function createOrUpdateOffer(): Promise<string | null> {
const offerData = {
sku,
marketplaceId: "EBAY_US",
format: "FIXED_PRICE",
availableQuantity: 10,
merchantLocationKey,
categoryId,
listingPolicies: {
fulfillmentPolicyId: process.env.EBAY_FULFILLMENT_POLICY_ID,
paymentPolicyId: process.env.EBAY_PAYMENT_POLICY_ID,
returnPolicyId: process.env.EBAY_RETURN_POLICY_ID,
},
pricingSummary: {
price: { currency: "USD", value: "99.99" },
},
};
const existingOfferId = await getOfferBySKU();
if (existingOfferId) {
try {
await ebay.sell.inventory.updateOffer(existingOfferId, offerData);
console.log("✅ Offer Updated:", existingOfferId);
return existingOfferId;
} catch (error) {
console.error("❌ Error updating offer:", error);
return null;
}
} else {
try {
const response = await ebay.sell.inventory.createOffer(offerData);
if (response?.offerId) {
console.log("✅ New Offer Created:", response.offerId);
return response.offerId;
}
return null;
} catch (error) {
console.error("❌ Error creating offer:", error);
return null;
}
}
}
// Publish an offer if it exists
async function publishOffer(offerId: string) {
try {
if (!offerId || typeof offerId !== "string") {
throw new Error("❌ Invalid offerId provided for publishing.");
}
const response = await ebay.sell.inventory.publishOffer(offerId);
console.log("✅ Offer Published:", response);
} catch (error) {
console.error("❌ Error publishing offer:", error);
}
}
// Execute full inventory and offer flow
(async () => {
await createOrReplaceInventoryItem();
const offerId = await createOrUpdateOffer();
if (offerId) {
await publishOffer(offerId);
}
})(); |
Beta Was this translation helpful? Give feedback.
Resolved
I found this doc of eBay developers program, followed it and successfully listed an item via eBay's REST API.
eBayListItem.ts
: