// FULL PRINTFUL AUTOMATION ENGINE
export default async (request) => {
const order = await request.json();
const line = order.line_items[0];
const props = line.properties;
const printfile = props["PrintFile"]; // base64 PNG
const text = props["Custom Text"];
// 1. Upload Print File to Printful
const uploadRes = await fetch("https://api.printful.com/files", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_PRINTFUL_API_KEY"
},
body: JSON.stringify({
filename: `design_${order.id}.png`,
contents: printfile.replace("data:image/png;base64,", "")
})
});
const uploaded = await uploadRes.json();
const fileId = uploaded.result.id;
// 2. CREATE PRINTFUL ORDER
const resp = await fetch("https://api.printful.com/orders", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_PRINTFUL_API_KEY"
},
body: JSON.stringify({
external_id: `${order.id}`,
shipping: "STANDARD",
recipient: {
name: `${order.shipping_address.first_name} ${order.shipping_address.last_name}`,
address1: order.shipping_address.address1,
city: order.shipping_address.city,
state_code: order.shipping_address.province_code,
country_code: order.shipping_address.country_code,
zip: order.shipping_address.zip,
email: order.email
},
items: [
{
sync_variant_id: YOUR_PRINTFUL_VARIANT_ID,
quantity: 1,
files: [
{
id: fileId, // The uploaded print file
type: "default"
}
]
}
]
})
});
const json = await resp.json();
return new Response(JSON.stringify({
status: "Printful Order Created",
printful: json
}), { status: 200 });
};