Importing product data into Wix via API

I’m having trouble with
I am trying to import product data from ssactivewear.com via their API. I am new to API’s but i

Working in
e.g. Wix Studio Editor, Wix Editor, Dev Mode

Site link
dev mode wix

What I’m trying to do
Trying to import product data from ss to create products/attributes in wix cms or store.

What I’ve tried so far
When i run a test i get a 500 error in dev mode. This is my .js backend file:

import { fetch } from ‘wix-fetch’; // Import the fetch API

import {getSecret} from ‘wix-secrets-backend’;

export const getApiKey = async ()=>{

const privateKey = await getSecret(“SSactivewearKey”);

return privateKey

}

// Import secrets manager if needed

export async function getExternalData() {

// Use the external API endpoint URL

const url = ‘https://api.ssactivewear.com/v2/brands/’;

// Example of adding headers (including authentication if needed)

const headers = {

'Content-Type': 'application/json',

'Authorization': 'Bearer ' + getApiKey() // Access secret here

};

try {

**const** response = **await** fetch(url, { method: 'GET', headers: headers });



**if** (!response.ok) {

  **throw** **new** Error(\`HTTP error! status: ${response.status}\`);

}



**const** data = **await** response.json(); // Parse the JSON response

**return** data; // Return the data to the frontend

} catch (error) {

console.error("API call failed:", error);

**return** { error: error.message };

}

}

Your 500 is happening because S&S Activewear doesn’t use Bearer auth. Their REST API uses Basic Authentication:

  • Username = Account Number
  • Password = API Key

Also, in your code you’re doing Bearer + getApiKey() without await, so you’re literally sending Bearer [object Promise].

3 Likes

Can you tell me the correct way to write the authorization part of the code and should it be in the header?

I have figured it out, thank you for your help

Ok, i got it to return the product data but now my “createWixProduct” function is getting this error:

Error creating Wix product:

{…}

jsonTableCopy JSON

message:

"Endpoint belongs to CATALOG_V1, but your site is using CATALOG_V3. Read more about catalog versioning: Introduction | API Reference "

details:

{…}

Could not create product in Wix Stores

Here is my code so far:

import { fetch } from ‘wix-fetch’;

import wixStoresBackend from ‘wix-stores-backend’;

import { getSecret } from ‘wix-secrets-backend’;

export async function fetchExternalProducts() {

**const** userName = **await** getSecret("SSactivewearUse");

**const** password = **await** getSecret("SSactivewearKey");

const url = ‘https://api.ssactivewear.com/v2/styles/Gildan 5000’;

const encodedCredentials = Buffer.from(userName + ‘:’ + password, ‘utf-8’).toString(‘base64’);

**const** headers = {

    'Content-Type': 'application/json',

    'Authorization': 'Basic ' + encodedCredentials, // Access secret here

};



**try** {

    **const** response = **await** fetch(url, { method: 'GET', headers: headers });



    **if** (!response.ok) {

        **throw** **new** Error(\`HTTP error! status: ${response.status}\`);

    }



    **const** data = **await** response.json(); // Parse the JSON response

    **return** data; // Return the data to the frontend

} **catch** (error) {

    console.error("API call failed:", error);

    **return** { error: error.message };

}

}

// Function to create a single product in Wix Stores

export async function createWixProduct(productData) {

// Map your API's product data structure to the Wix ProductInfo object

**const** wixProduct = {

    name: "productData.styleName",

    price: 0,

    

};



**try** {

    // This function requires elevated permissions to run in the backend

    **const** createdProduct = **await** wixStoresBackend.createProduct(wixProduct);

    **return** createdProduct;

} **catch** (error) {

    console.error('Error creating Wix product:', error);

    **throw** **new** Error('Could not create product in Wix Stores');

}

}

// Exported function that the frontend will call

export async function importProductsFromApi() {

**try** {

    **const** productsToImport = **await** fetchExternalProducts();

    **const** createdProducts = \[\];



    **for** (**const** productData **of** productsToImport) {

        **const** newProduct = **await** createWixProduct(productData);

        createdProducts.push(newProduct);

    }

    

    **return** { success: **true**, count: createdProducts.length, products: createdProducts };



} **catch** (error) {

    **return** { success: **false**, message: error.message };

}

}