How to create a Product with images in Swell using backend API in a NextJS server action ? #536
-
I really would like to see a more complete example in the backend documentation on how to create a product where an explanation on how to upload images is more clear. In my NextJS app, I'm receiving const files = formData.getAll("service_image_file") as File[]; Then, I'm looping over the const arrayBuffer = await file.arrayBuffer();
const buffer = new Uint8Array(arrayBuffer); and then I'm trying to create my Product (which I'm calling Service) const { id } = loggedInSwellUser?.results[0];
// save the data in Swell
const serviceDraft = await createProductDraft({
name: parsed.data.service_name,
price: parsed.data.service_price,
description: parsed.data.service_description,
images: finalImages, // this SHOULD HAVE MY ARRAY of Buffer images
vendor_id: id,
}); this is how the import "server-only";
import swell from "@/lib/server";
export const createProductDraft = async (productData: any) => {
const res = await swell.post("/products", {
name: productData.name,
price: productData.price,
description: productData.description,
images: productData.images,
// TODO: this is a draft, make sure to change this to false later
active: true,
type: "digital",
vendor_id: productData.vendor_id,
});
return res;
}; But the images are not created, not uploaded. I just don't know what's going on. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
First, you need to upload a list of files using Then, you can create a product with |
Beta Was this translation helpful? Give feedback.
First, you need to upload a list of files using
/:files
endpoint.https://developers.swell.is/backend-api/files/create-a-file#create-a-file
Then, you can create a product with
images
field value as the list of file upload responses.https://developers.swell.is/backend-api/products/create-a-product