Netlify Blobs: accessing from function (inside SvelteKit app) requiring explicit token, siteId, deployId

I’m exploring using Netlify Blobs (which I’m aware is in Beta) within my site.

In a local build plugin, I generate a pile of JSON, and set it to a deploy-specific store (using getDeployStore. In this plugin, I have to pass in these credentials to make it work:

  const store = getDeployStore({
    siteID: constants.SITE_ID,
    token: constants.NETLIFY_API_TOKEN,
    deployID: process.env.DEPLOY_ID,
  await store.setJSON('posts', postData);

This works, and populates the store at build time!

I then attempt to access it like so:

    const store = getDeployStore();
    const unsorted:Post[] = await store.get('posts', {type: 'json'});

I’m building a site in SvelteKit; this code gets translated to a Netlify Function, where it runs. If I run the above function, I get the following error:

Runtime.UnhandledPromiseRejection - MissingBlobsEnvironmentError: The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: deployID

The documentation suggests getDeployStore should Just Work™, but it appears to rely on process.env being available to the function.

Now: my gut says that because I’m running Sveltekit with adapter-netlify, and the build process is managed by Vite, process.env is not available; instead, I should import environment variables from SvelteKit’s own $env/dynamic/private or similar.

If I import DEPLOY_ID from $env/dynamic/private, and pass it by hand into getDeployStore, I now get a new error:

Runtime.UnhandledPromiseRejection - MissingBlobsEnvironmentError: The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: siteID, token

siteID is easily found in the environment variables, but it appears the code would also like me to supply a full-access Netlify Token. If I generate a full-access PAT, store it as a dedicated environment variable, and then use it in the function to configure my store… everything works. But this feels like a sledgehammer to crack a nut.

How can I specify the token manually inside my function - it feel slike it should “magically” be available inside the function context, and yet it’s unclear how to retrieve it.

Is this happening in Netlify CLI or on the hosted app? There’s already an internal issue to remove this error from the CLI.

This is an error logged to logs/browser when the function is called. It doesn’t have access to the API key, I believe because process.env has been stripped by Vite.

I can make this work by including a PAT manually, but that feels like the incorrect approach based on all documentation.

Thank you for sharing, I’ve passed this on to the devs.

1 Like

The only clue I have - which may be useful to the devs, @hrishikesh - is that the environment variable NETLIFY_BLOBS_CONTEXT appears to be unavailable at both build and runtime for these functions; this line in the blobs library suggests it (or the variable found in globalThis is required), and whenever I build or run the site, this always resolves to undefined.

OK, I have a bit more info that might help.

  • my site is built using SvelteKit’s adapter-netlify. This builds a bunch of pages as static, and turns the dynamic pages into Netlify Functions. (The details of this, which turn out to be important, are something I’m vague on right now).
  • As a test, I made a SvelteKit endpoint that would log environment variables - this endpoint would get turned into a Netlify Function at build time.
  • I also made a manual function in netlify/functions to do the same.

There are two environment variables the function in netlify/functions has available to it that the SvelteKit endpoint does not: NETLIFY_PURGE_API_TOKEN and NETLIFY_BLOBS_CONTEXT.

So that would explain why Blobs aren’t working inside the SvelteKit-generated function; the context is entirely missing. What I don’t currently understand is why those environment variables are missing: I assumed both functions would be running inside the same environment.

I’ll see if I can reach out to SK maintainers to work out what’s going on.

Thanks for the deep-dive. I have passed it on to the devs.

I have the same issue with Next.js.
Any updates?

According to blobs/src/environment.ts at main · netlify/blobs · GitHub there should be NETLIFY_BLOBS_CONTEXT variable.

  1. I have Next.js website.
  2. I have API page route
export default function handler(req, res) {

      test: 'test',
      environment: process.env.NODE_ENV,
      env: JSON.stringify(process.env),
      netlify_blobs: process.env.NETLIFY_BLOBS,
      test2: 'test2',

And there is no NETLIFY_BLOBS variable in the output…

  "AWS_SESSION_TOKEN": "....",
  "LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
  "LAMBDA_TASK_ROOT": "/var/task",
  "AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/d8b33cea294a7d99410f6782b13a8148dcc31166c053ccc8d289564ad8e9bc2b",
  "AWS_LAMBDA_LOG_STREAM_NAME": "2024/02/22/[$LATEST]d1a7fd63a236444c83d8cc9ed106062e",
  "AWS_EXECUTION_ENV": "AWS_Lambda_nodejs18.x",
  "SITE_ID": "19c948ef-1830-429c-95af-0fde50f897dc",
  "AWS_LAMBDA_FUNCTION_NAME": "d8b33cea294a7d99410f6782b13a8148dcc31166c053ccc8d289564ad8e9bc2b",
  "PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
  "AWS_DEFAULT_REGION": "us-east-2",
  "PWD": "/var/task",
  "AWS_SECRET_ACCESS_KEY": "Lrl9onnQsEw23uiottrTmf9V/AyDT5KqcTEV57vH",
  "LANG": "en_US.UTF-8",
  "LAMBDA_RUNTIME_DIR": "/var/runtime",
  "NODE_PATH": "/opt/nodejs/node18/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task",
  "TZ": ":UTC",
  "AWS_REGION": "us-east-2",
  "URL": "",
  "SHLVL": "0",
  "_AWS_XRAY_DAEMON_PORT": "2000",
  "_HANDLER": "___netlify-handler.handler",
  "SITE_NAME": "jss-nextjs-hosting",
  "NODE_EXTRA_CA_CERTS": "/var/runtime/ca-cert.pem",
  "NODE_ENV": "production",
  "PUBLIC_URL": "",
  "_X_AMZN_TRACE_ID": "Root=1-65d78c93-5c5400673898ab526c80658c;Parent=017ac75363729ae4;Sampled=0;Lineage=dd45a4d6:0",
  "__NEXT_PROCESSED_ENV": "true",
  "FETCH_WITH": "GraphQL",
  "__NEXT_OPTIMIZE_FONTS": "true",

Does Next.js pages API route have ability to use Blobs?
It works with siteID and token. But is it expected that I need to configure them for runtime?
If yes, then it should be specified in the documentation.

No duplicates please @Antonytm: Environment variables inconsistency

For v4 of Next.js Runtime, yes for both questions.

I wasn’t able to find it in the documentation. I checked:
Netlify Blobs | Netlify Docs (Blobs)
next-runtime/ at main · netlify/next-runtime · GitHub (next runtime)

Can you point me out, where can I find it, please?

Next Runtime v4 was not made in the time when Blobs existed. And when Blobs were made, work on Next Runtime v5 had already begun in which case updating the documentation for a niche use-case was not the highest priority. Most of what you need is documented here anyways: blobs/ at main · netlify/blobs (