Question:
I am trying to insert data into a table in the CMS
Permissions and Privicy on the table are Set to view and add for everyone
My API key is correct
my Data being sent is …
Data to be sent to Wix CMS: {
“dataCollectionId”: “Affiliates”,
“dataItem”: {
“id”: “nywh46x”,
“data”: {
“contactName”: “Rodney Allen”,
“contactEmail”: “rakess70@gmail.com”,
“referralCode”: “nywh46x”
}
}
}
My header is:
Headers for Wix CMS request: {
“Authorization”: “Bearer [MYCORRECT KEY IS HERE]”,
“Content-Type”: “application/json”
}
This is my Function in my APP
// Function to insert user data into Wix CMS
async function insertAffiliateData(name: string, email: string, referralCode: string) {
try {
const data = {
dataCollectionId: ‘Affiliates’,
dataItem: {
id: referralCode,
data: {
contactName: name,
contactEmail: email,
referralCode: referralCode,
},
},
};
const headers = {
Authorization: `Bearer ${process.env.WIX_API_KEY}`,
'Content-Type': 'application/json',
};
console.log("Data to be sent to Wix CMS:", JSON.stringify(data, null, 2));
console.log("Headers for Wix CMS request:", JSON.stringify(headers, null, 2));
const response = await axios.post(
'https://www.wixapis.com/wix-data/v2/items',
data,
{ headers }
);
console.log("Wix CMS Response:", response.data);
} catch (error: any) {
// Type guard to check if error is an Axios error
if (error.response && error.config) {
console.error(“Error inserting data item:”, error.message);
console.error(“Axios Error Details:”, error.response.data || error.toJSON());
} else {
console.error(“Unknown error inserting data item:”, error);
}
}
}
Product:
Wix Data
What are you trying to achieve:
I am simply trying to add a record to my Wix CMS via a Next JS APP
What have you already tried:
I have tried to use the documentation and Search feature to answer this
Additional information:
This is the first time I am trying to interface with a wix system so any help would be greatly welcomed