Netlify Blobs in different environments

I’m having some problems with blobs, specifically across different environments. Originally I was doing this:

const store = getStore({ name, token, siteID });

But I realized that was always putting the blob in the Netlify UI (prod). Instead, I wanted to work with blobs in dev until I was ready for them to be in prod. The @netlify/blobs npm package says:

To do this, the system that holds the configuration data should set a global variable called netlifyBlobsContext or an environment variable called NETLIFY_BLOBS_CONTEXT with a Base64-encoded, JSON-stringified representation of an object with the following properties:

  • apiURL (optional) or edgeURL: URL of the Netlify API (for API access) or the edge endpoint (for Edge access)

  • token: Access token for the corresponding access mode

  • siteID: ID of the Netlify site

This data is automatically populated by Netlify in the execution environment for both serverless and edge functions.

With this in place, the getStore method can be called just with the store name. No configuration object is required, since it’ll be read from the environment.

I did those steps and now have a NETLIFY_BLOBS_CONTEXT in my Netlify UI, the site is linked with netlify link. When I run netlify dev –verbose I’m expecting to see the new environment variable in the console. I see all the other ones except this one.

Then when I start using blobs like this:

const store = getStore(name);

I get this:

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

Another thing I tried, I moved all of the getStore calls within functions. In the beginning I had the call outside at the top of the file. That was working when I set the token and siteID directly in the getStore function. I figured I’d try moving to see if it did anything. But from what I can see, it looks like the NETLIFY_BLOBS_CONTEXT isn’t being read in netlify dev. What do I need to do?

Edit: I’ll also mention that I did a netlify unlink and the netlify link to see if I needed to relink the site to get the variables to populate. I also tried console logging the process.env.NETLIFY_BLOBS_CONTEXT and it’s undefined.

Edit2: netlify env:list DOES show the variable and so does netlify env –debug. Even after running those commands, it doesn’t work (doesn’t show in netlify dev when running).

If I understand correctly, you simply want to use Netlify Blobs in CLI without writing to production data? If so, the CLI does that by default when tthe site is unlinked and you don’t need any variables.

So, you’re saying I should unlink the site for this to work? That doesn’t sound right. I’m not sure how I’m supposed to bring in variables to the other parts of my site then. All of my variables are listed in the Netlify UI. I don’t want to have to list them in some .env file AND in the Netlify UI.

Is there no other way to use blobs without a siteID and token?

I just found this post where you said you don’t need to set up the environment variables, they just get picked up. I tried removing the original ones that I set, thinking that maybe they were conflicting. I still get the same error.

I’ll add one more thing, just in case it matters somehow. The store is being used in files that aren’t themselves edge functions. Instead they are used in files that are imported into edge functions.

// file1.js

const store = getStore(name)

export function doSomething() {
  return store.get(fileName)
}

Then in the edge function handler

import { doSomething } from './file1.js';

export default async function handler() {
  const data = await doSomething();
}

If this is a problem, where stores can’t be used in files outside the handler, then it’s not in the docs. Also, like I mentioned before, it works when I supply the siteID and token directly. Everything only breaks when you don’t supply these things and everything is meant to “just work” between dev and prod. Making the fundamental setup overall different between dev and prod.

For the time being I’ve made a small utility that seems to be ok for now. But I’d really appreciate some support on this.

import { getStore } from '@netlify/blobs';

const devStore = new Map();

export function useStore() {
    if (process.env.NETLIFY_DEV) {
        return devStore;
    } else {
        return getStore(name);
    }
}

In that case, I believe you need to remove the `getStore() function form the global scope, something like:

const store = () => getStore('name')

Then you can:

await store().get('...')

That doesn’t work. All I did was change my function above to always return the getStore() instead of sometimes returning the map. It fails.

Solved. I was writing my functions in what is called “Lambda compatibility mode”. So either you need to use the connectLambda export from the blobs package or rewrite your handlers to use Request/Response. I went with the latter, and it is now working as expected.

I don’t know what the best way to raise this is in documentation because it’s just weird that the Request/Response structure is related to blobs that aren’t being used directly by the handler.