Hello,
I've been trying to leverage the Migration API to do bulk document creations: Migration API Technical Reference - Documentation - Prismic
It was working 6 months ago on another project - tried to follow the same process, in vain. I keep getting errors with the authentication:
such as:
{"message":"Invalid key=value pair (missing equal-sign) in Authorization header (hashed with SHA-256 and encoded with Base64): 'xxx='."}%
or:
'x-amzn-errortype': [ 'MissingAuthenticationTokenException' ],
--
script:
const keywords = ["comment régler un frigo", "comment rénover une maison"];
const createDocuments = async () => {
for (const keyword of keywords) {
try {
const document = {
format: "page",
type: "guide_child",
uid: keyword.replace(/\s+/g, "-").toLowerCase(),
data: {
title: keyword,
slices: [],
},
seo: {
meta_title: keyword,
meta_description: `Find out more about ${keyword}`,
},
};
console.log("Sending document:", JSON.stringify(document, null, 2));
const headers = {
"Content-Type": "application/json",
// Authorization: `Bearer ${API_TOKEN}`,
"x-api-key": API_TOKEN,
repository: REPOSITORY,
};
console.log("Request Headers:", headers);
const response = await fetch(API_URL, {
method: "POST",
headers,
body: JSON.stringify({ documents: [document] }),
});
console.log("Response Status:", response.status);
console.log("Response Headers:", response.headers.raw());
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to create document for "${keyword}": ${errorText}`,
);
}
const responseData = await response.json();
console.log(
`Document for "${keyword}" created successfully:`,
responseData,
);
} catch (error) {
console.error("Error creating document:", error.message);
}
}
};
createDocuments();
I'm a bit confused on the process to follow, would love a step by step or clear example in the documentation.
am I supposed to use a Write API token That I'll add in my settings > API & Security?
Or do I need to use the access key provided on the doc Migration API Technical Reference - Documentation - Prismic
Thanks for the help!