Build failing due to 401 error

9:32:38 AM: Netlify Build                                                 
9:32:38 AM: ────────────────────────────────────────────────────────────────
9:32:38 AM: ​
9:32:38 AM: ❯ Version
9:32:38 AM:   @netlify/build 29.33.2
9:32:38 AM: ​
9:32:38 AM: ❯ Flags
9:32:38 AM:   baseRelDir: true
9:32:38 AM:   buildId: 65bb65146bcb2900081dd26b
9:32:38 AM:   deployId: 65bb65146bcb2900081dd26d
9:32:38 AM: ​
9:32:38 AM: ❯ Current directory
9:32:38 AM:   /opt/build/repo
9:32:38 AM: ​
9:32:38 AM: ❯ Config file
9:32:38 AM:   No config file was defined: using default values.
9:32:38 AM: ​
9:32:38 AM: ❯ Context
9:32:38 AM:   production
9:32:38 AM: ​
9:32:38 AM: Build command from Netlify app                                
9:32:38 AM: ────────────────────────────────────────────────────────────────
9:32:38 AM: ​
9:32:38 AM: $ npm run build
9:32:38 AM: > unfold@0.0.1 build
9:32:38 AM: > astro build
9:32:39 AM: 09:32:39 [build] output: "static"
9:32:39 AM: 09:32:39 [build] directory: /opt/build/repo/dist/
9:32:39 AM: 09:32:39 [build] Collecting build info...
9:32:39 AM: 09:32:39 [build] ✓ Completed in 307ms.
9:32:39 AM: 09:32:39 [build] Building static entrypoints...
9:32:41 AM: 09:32:41 [build] ✓ Completed in 1.32s.
9:32:41 AM:  building client (vite) 
9:32:41 AM: 09:32:41 [vite] transforming...
9:32:41 AM: 09:32:41 [vite] ✓ 16 modules transformed.
9:32:41 AM: 09:32:41 [vite] rendering chunks...
9:32:41 AM: 09:32:41 [vite] computing gzip size...
9:32:41 AM: 09:32:41 [vite] dist/_astro/page.zjCbv0E3.js       0.72 kB │ gzip:  0.46 kB
9:32:41 AM: 09:32:41 [vite] dist/_astro/hoisted.hgVmI-so.js  121.85 kB │ gzip: 48.74 kB
9:32:41 AM: 09:32:41 [vite] ✓ built in 455ms
9:32:41 AM:  generating static routes 
9:32:41 AM: 09:32:41 ▶ src/pages/index.astro
9:32:41 AM: 09:32:41   └─ /index.html{"message":"Unauthorized","status":401,"response":"Unauthorized","id":"src/pages/index.astro"}
9:32:41 AM:   Hint:
9:32:41 AM:     To get as much information as possible from your errors, make sure to throw Error objects instead of `object`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error for more information.
9:32:41 AM:   Stack trace:
9:32:41 AM:     at createSafeError (file:///opt/build/repo/node_modules/astro/dist/core/errors/utils.js:64:19)
9:32:41 AM:     at cli (file:///opt/build/repo/node_modules/astro/dist/cli/index.js:152:11)
9:32:41 AM: ​
9:32:41 AM: "build.command" failed                                        
9:32:41 AM: ────────────────────────────────────────────────────────────────
9:32:41 AM: ​
9:32:41 AM:   Error message
9:32:41 AM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
9:32:41 AM: ​
9:32:41 AM:   Error location
9:32:41 AM:   In Build command from Netlify app:
9:32:41 AM:   npm run build
9:32:41 AM: ​
9:32:41 AM:   Resolved config
9:32:41 AM:   build:
9:32:41 AM:     command: npm run build
9:32:41 AM:     commandOrigin: ui
9:32:41 AM:     publish: /opt/build/repo/dist
9:32:41 AM:     publishOrigin: ui
9:32:42 AM: Failed during stage "building site": Build script returned non-zero exit code: 2
9:32:42 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
9:32:42 AM: Failing build: Failed to build site
9:32:42 AM: Finished processing build request in 24.514s

I’m having an issue deploying my Astro site, which builds locally just fine. I’m using settings copied from the docs in the index.astro file:

import { useStoryblokApi } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";
import BaseLayout from "../layouts/BaseLayout.astro";

const storyblokApi = useStoryblokApi();
const { data } = await storyblokApi.get("cdn/stories/home", {
  version: import.meta.env.DEV ? "draft" : "published",
});

I’ve got the right access token, set to ‘preview’. Everything works fine locally. Is there something simple I’ve missed?

@MrJonoCES You may already be aware, but as it’s a status of 401 indicating Unauthorized it indicates that it’s the response that’s being received from Storyblok when it’s trying to connect.

It may not be the ultimate problem, but I’d double check to make sure that it does have access to the correct access credentials / api token at the point that it’s used.

Thanks. Other than checking the access token, which is 100% correct, what else could that be failing on?

@MrJonoCES I can’t think of much off the top of my head.

The nature of the error indicates that communication is being made with the other side (Storyblok) but that they’re sending back a response (the 401).

So I don’t believe it to be a connection issue.

Because you are leaning on various plugins it’s possible they return a 401 in some situation other than an actual 401, but not probable.

Heads up that I don’t work for Netlify, nor do I use Astro, so I’m only speaking from experience as a dev with almost 20 years professional experience.

Thanks Nathan. I found the problem, my complete lack of experience haha! I’d forgotten that the environment variables don’t get included so you have to enter them on the Netlify page as well. Doh!

1 Like

@MrJonoCES Great to see that double checking the credentials was the right answer.

Best of luck on your continued journeys.

1 Like