PrismicError: An invalid API response was returned

Hello! Since Friday from last week I have been getting an error due to "An invalid API response was returned".
With this error it is not possible to build the website.

I also saw that the slice-machine version for this project was old so I updated it. The "sm.json" file was deprecated so when I updated the slice-machine package to the latest version the "slicemachine.config.json" file was created successfully, but as a consequence of this update I lost a good amount of slices... The slice-machine localhost does not detect some slices after this update, even though, I can still see them and their corresponding "model.json" file in my code base.

Any further help with this problems would be greatly appreciated.

Thank you in advance,
Nuno

@nunopereirasousa00,
It has been my experience that when you upgrade your packages and then run dev, their script does create the appropriate slicemachine.config.json file as you mentioned, but it does not then adjust your prismicio.js

Check that your prismicio looks similar to the following:

import * as prismic from '@prismicio/client'
import * as prismicNext from '@prismicio/next'
import config from './slicemachine.config.json'

/**
 * The project's Prismic repository name.
 */
export const repositoryName = config.repositoryName
//
// your route stuff goes here...
//
export const createClient = (config = {}) => {
  const client = prismic.createClient(repositoryName, {
    routes,
    ...config,
  })

  prismicNext.enableAutoPreviews({
    client,
    previewData: config.previewData,
    req: config.req,
  })

  return client
}

Your slicemachine.config.json should be similar to this:

{
  "apiEndpoint": "https://your-repo-here.cdn.prismic.io/api/v2",
  "repositoryName": "your-repo-here",
  "adapter": "@slicemachine/adapter-next",
  "libraries": ["./slices"],
  "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
}

Does that help in any way?

1 Like

@nf_mastroianni thank you for the response! Seems like in the code I had "config.apiEndpoint" and not "config.repositoryName". I can't really check atm if the "An invalid API response was returned" error is fixed with this change because I still have a lot of slices missing. Meaning if I build the website again, if the error no longer exists, the website will be deployed without the missing slices.
Regarding this issue, when I am at the "Changes" tab in http://localhost:9999/slices, sometimes one of the missing slices appears and then suddenly disappears. Is there any way to get these back? As I said before, I can still see the missing slices in the "slices" folder in my code base, just not at http://localhost:9999/slices. I don't know if this might be connected, but right after I updated the slice machine in this project, the localhost loses connection with the Prismic authentication quite often.

Wow, that seems very strange about how they appear and disappear like that. It is my understanding that if the data are present in your models files, you should be safe. Could this be a browser/caching issue? If you cleared your cache or tried another browser? Also, when strange things like this happen, I've found that deleting the .next directory and then running dev again helps.

Already cleared cache and deleted the .next folder and still the same.
Yes it is super weird... basically when I click for the first time in the "Changes" tab sometimes it shows me either a slice with the "New" tag, which was created and published months ago, or the missing slices. If I click refresh they disappear from the "changes" tab right away.

Have you tried spinning up a new prismic repo and project using

prismic theme --theme-url https://github.com/yourGitHubUserName/YourGitHubRepoName

Might be worth it to see if the slices/types are all faithfully recreated

I was able to get the slices back. I installed again the slice-machine version I had initially - 0.5.2 - and the slices missing appeared, but I still see "An invalid API response was returned" when building the website. Could this be related to the 'Your request count (151) is over the allowed limit of 150.'? (as it shown on the first image I uploaded). I already have the prismicio.js file as you have it above.

@nunopereirasousa00,
I'm happy to see you were able to resolve the missing slices issue. I've not seen a error message like you describe.

Is the error related just to your sitemap? I feel like it's really hard to figure this out with the info that I have.

1 Like

Yes it was the sitemap file. For some reason there was "config.apiEndpoint" instead of "config.repositoryName" in this file. Not sure why this error appeared after successfully building the website so many times for so long with "config.apiEndpoint".
Anyway, thank you so much @nf_mastroianni!

1 Like

Hey there,
I have a pretty weird Error, this error started, after creating a new Page Type. So I removed the Page Type again in the Sliceeditor and pushed it to the Server.
I also removed all the Code I created (just went one commit back)...

Now I get this Error:

PrismicError: An invalid API response was returned
    at Client.fetch (webpack-internal:///(rsc)/./node_modules/@prismicio/client/dist/createClient.js:1163:19)       
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Client.getFirst (webpack-internal:///(rsc)/./node_modules/@prismicio/client/dist/createClient.js:277:24)
    at async Client.getSingle (webpack-internal:///(rsc)/./node_modules/@prismicio/client/dist/createClient.js:533:16)
    at async Module.generateMetadata (webpack-internal:///(rsc)/./src/app/page.tsx:29:18) {
  url: 'https://myproject.cdn.prismic.io/api/v2/documents/search?q=%5B%5Bat%28document.type%2C+%22home%22%29%5D%5D&pageSize=1&ref=ZbJl-xEAAB8A-iZe&routes=%5B%7B%22type%22%3A%22home%22%2C%22path%22%3A%22%2F%22%7D%2C%7B%22type%22%3A%22page%22%2C%22path%22%3A%22%2F%3Auid%22%7D%2C%7B%22type%22%3A%22neuigkeiten_post%22%2C%22path%22%3A%22%2Fneuigkeiten%2F%3Auid%22%7D%2C%7B%22type%22%3A%22neuigkeiten%22%2C%22path%22%3A%22%2Fneuigkeiten%2Fpage.tsx%22%7D%5D',
  response: undefined
}

I use Next Js:
my Configuration Files are all right.
I tried:

  • Updating all Packeges
  • Restarting my PC
  • Updated Node js
  • Connecting my Project again with the Prismic project (npx @slicemachine/init@latest --repository my-project)

Here are the other files:

{
  "repositoryName": "my-project",
  "adapter": "@slicemachine/adapter-next",
  "libraries": ["./src/slices"],
  "localSliceSimulatorURL": "http://localhost:3000/slice-simulator"
}
import * as prismic from "@prismicio/client";
import * as prismicNext from "@prismicio/next";
import config from "../slicemachine.config.json";

/**
 * The project's Prismic repository name.
 */
export const repositoryName =
  process.env.NEXT_PUBLIC_PRISMIC_ENVIRONMENT || config.repositoryName;

/**
 * A list of Route Resolver objects that define how a document's `url` field is resolved.
 *
 * {@link https://prismic.io/docs/route-resolver#route-resolver}
 */
// TODO: Update the routes array to match your project's route structure.
const routes: prismic.ClientConfig["routes"] = [
  {
    type: "home",
    path: "/",
  },
  {
    type: "page",
    path: "/:uid",
  },
  {
    type: "neuigkeiten_post",
    path: "/neuigkeiten/:uid",
  },
  {
    type: "neuigkeiten",
    path: "/neuigkeiten/page.tsx",
  },
];

/**
 * Creates a Prismic client for the project's repository. The client is used to
 * query content from the Prismic API.
 *
 * @param config - Configuration for the Prismic client.
 */
export const createClient = (config: prismicNext.CreateClientConfig = {}) => {
  const client = prismic.createClient(repositoryName, {
    routes,
    fetchOptions:
      process.env.NODE_ENV === "production"
        ? { next: { tags: ["prismic"] }, cache: "force-cache" }
        : { next: { revalidate: 5 } },
    ...config,
  });

  prismicNext.enableAutoPreviews({
    client,
    previewData: config.previewData,
    req: config.req,
  });

  return client;
};

@carlo2 The error is caused by the content API being down. The Prismic team says they are in contact with AWS about resolving the downtime. Keep an eye on the status page and this thread:

3 Likes

Oh, so it's about the server! haha good to know I was looking everywhere to figure out what is going on...

1 Like

Oh, thanks for the heads up! I've been searching high and low for a solution, even asking ChatGPT for help, haha! :sweat_smile:

For anyone reading this thread in the future and encountering a similar error, here is the Statuspage ;-)

Prismic Status Page

It's a bit of a relief to know it's an external issue, although it's still frustrating to hit a snag like this. Good to have a community to share these updates with. :smile:

1 Like

Main thread updated.