Migration API POST - Not working anymore - Auth issues

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!

Getting the same error message above invalid key=value pair (missing equal-sign) in Authorization header (hashed with SHA-256 and encoded with Base64 When trying to do a POST method

const response = await fetch(url, {
      headers: {
        Authorization: `Bearer ${token}`,
        "x-api-key": apiKey,
        "Content-Type": "application/json",
        repository: repository,
      },
      method: "POST",
      body: JSON.stringify(doc),
    });

Hi,

Thanks for the report.

Could you please give us the "API_URL" / "url" used for the "fetch"?
We are able to reproduce by not having the correct URL, so it may be related to that.
Example:

POST https://migration.prismic.io/document // BAD
POST https://migration.prismic.io/documents // GOOD

Note that today, you need to specify both "x-api-key" and "Authorization" header. We are working on a solution so only "Authorization" would be necessary, but it's not yet deployed.

Xavier

Hi @xavier.rutayisire can confirm that I am using https://migration.prismic.io/documents/ with both "x-api-key" and "Authorization" in the header

I was not able to reproduce on my end :confused:

It seems that each time I have an incorrect URL I can trigger the exact same message but never otherwise. The URL is case-sensitive and also even a space at the end can create the error.

Could you check that on your end? Something wrong on the call, URL, method, headers, anything that seems off. Don't hesitate to try from Postman or another tool to see if it can come from the script setup.

Also, if you can please tell me your repository name, I could try to look for logs.

Xavier