Write API returning 404 "model not found"

Hi, I’m trying to bulk add content using the Write API without success. No matter what model name I pass in the write api url (https://builder.io/api/v1/write/MODEL) I always get 404 error with “Model not found”. I followed the guide here and try with different models I have and it’s always the same. I’ll show a quick sample of this using my Document Meta model:

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", `Bearer ${apiKey}`); // This is a private key (bpk-...)

var raw = JSON.stringify({
  ownerId: "f127073fccdc44a590bf4362d8bb100b",
  "@version": 3, // also tried with 2 here
  name: "document-meta test",
  modelId: "0f01a5b1a6374a7baaefb17775a141ee",
  published: "draft",
  data: {
    title: "this is a title",
    description: "entry.description",
    openGraphImage:
      "https://cdn.builder.io/api/v1/image/assets%2Ff127073fccdc44a590bf4362d8bb100b%2F00cbd27d8d5d482f86e6d93c35306230",
    twitterCardImage:
      "https://cdn.builder.io/api/v1/image/assets%2Ff127073fccdc44a590bf4362d8bb100b%2F9ade3b32a42b4d5da643f17fe54b3284",
    canonicalUrl: "https://sporttrade.io/",
  },
});

var requestOptions = {
  method: "POST",
  headers: myHeaders,
  body: raw,
  redirect: "follow",
};

fetch("https://builder.io/api/v1/write/document-meta", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error));

Also rewrite it simpler like this:

async function importToBuilder() {
  const body = {
    name: "sample entry",
    modelId: "0f01a5b1a6374a7baaefb17775a141ee",
    published: "published",
    data: {
      title: "this is a title",
      description: "entry.description",
      openGraphImage:
        "https://cdn.builder.io/api/v1/image/assets%2Ff127073fccdc44a590bf4362d8bb100b%2F00cbd27d8d5d482f86e6d93c35306230",
      twitterCardImage:
        "https://cdn.builder.io/api/v1/image/assets%2Ff127073fccdc44a590bf4362d8bb100b%2F9ade3b32a42b4d5da643f17fe54b3284",
      canonicalUrl: "https://spor.io/",
    },
  };
  console.log("Importing entry:", body);

  try {
    const response = await fetch(
      `https://builder.io/api/v1/write/document-meta`,
      {
        method: "POST",
        headers: {
          Authorization: `Bearer ${apiKey}`, // This is a private key (bpk-...)
          "Content-Type": "application/json",
        },
        body: JSON.stringify(body),
      }
    );

    const data = await response.json();
    console.log("Imported entry:", data);
  } catch (error) {
    console.error("Error importing entry:", error);
  }
}

 importToBuilder();

I really don’t know what else can I try so any help is really appreciated.

Builder public api key
f127073fccdc44a590bf4362d8bb100b

Code stack you are integrating Builder with
Gatsby

Hello @lucas_aven,

Are you using the public key or private key in the auth header?

Hi @manish-sharma thx for the quick response! I’m using a private key (the one that starts with bpk-xxx

Hello @lucas_aven,

I attempted to use your code with private-key from your store and had no issues, I recommend you verify the private key and check if the issue is with the node version, I used node 18 to execute the script


Thank you a lot @manish-sharma I double checked and the issue is the following (Important to take into account since I think is a bug) even though I had the correct private api key I had the role of Developer and that was causing the issue for what I can tell. As soon as I was promoted to Admin I was able to make it work with the same private key i was using when I was Developer.