Trouble using Blobs and Identity in the same function

I’m pretty new to Netlify, so please forgive me if I’m missing something that should be obvious, but I’m having trouble both accessing Identity context and using Blobs from the same Function.

A couple of days ago I implemented a couple of functions to save a Blob and to get a Blob. Let’s take a simplified version of the get a blob function as an example:

import { getStore } from "@netlify/blobs"

export default async (req, context) => {
  const itemId = new URL(req.url).searchParams.get('id')

  const items = getStore("items")
  const item = await items.get(itemId, { type: 'json' })
  if (!item) {
    return new Response('no item found with id', {
      status: 404
    })
  }

  return new Response(JSON.stringify(item))
}

These two functions have been working just fine for me. But now I want to start to associate these blob items with an Identity user using their sub as a blob key.

I was unable to find a way to get Identity information from the function as written above. Despite me including the proper authorization header in my requests, the context variable never had any user information for me. Which makes sense because the Functions and Identity docs mention a clientContext property but the context object docs don’t mention it. However, the Lambda compatibility for Functions docs mention that the context will contain Identity information if it is passed.

So I implemented a new function using the Lambda-compatible format to fetch Identity information from the context.

const handler = async function (req, ctx) {
  const user = 
    (!('clientContext' in ctx) || !('user' in ctx.clientContext))
    ? null : ctx.clientContext.user

  if (user === null) {
    return {
      statusCode: 200,
      body: JSON.stringify({message: 'hello anonymous'})
    }
  } else {
    return {
      statusCode: 200,
      body: JSON.stringify({message: `hello, ${user.email}`})
    }
  }
}

export { handler }

And that works just fine.

So then I went to go move the Blobs functionality into the Lambda-compatible function and I started getting an error.

import { getStore } from "@netlify/blobs"

const handler = async function (event, ctx) {
  const items = getStore("items")
  const item = await items.get('static-item-id', { type: 'json' })
  if (!item) {
    return {
      body: 'no item found with id',
      statusCode: 404
    }
  }

  return {
    body: JSON.stringify(item),
    statusCode: 200
  }
}

export { handler }

This function in both local using netlify dev and a preview deploy throws an error on getStore: 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

Searching this forum and chatting with the AI bot seem to suggest I need to supply a Netlify API key, but I haven’t been able to successfully do that - most of the examples seem to be for build functions which I am not doing. And even if I could get that working, that doesn’t seem like a good solution.

So…am I missing anything here? Is there a way to get Identity information from a non-Lambda function or use Blobs from a Lambda function so that I can do both of these things together?

Hopefully I’ve provided enough info as this doesn’t seem to be site specific, but please let me know if I can provide anything else to assist in troubleshooting.

Thanks!

Once you run netlify link in the CLI, you should be able to access blobs.

Hi hrishikesh,

Thanks for the response, however that doesn’t solve my issue.

As mentioned in my initial post, Blobs works fine for me when using the non-Lambda-compatible Functions format, even locally but also in deployed environments. So I have already run netlify link. And when I try to use Blobs with the Lambda compatible Functions format it doesn’t work even once deployed to a preview environment, so netlify link has nothing to do with that as it is a deployed environment.

Do you have any other thoughts about how I could use Blobs with the Lambda-compatible Function format or how I could use Identity with the non-Lambda compatible Function format? Sorry if my question is unclear, I don’t have a better way to describe the different Functions formats as the docs don’t seem to give them official names nor officially state any limitations they may have.

Sorry, I did not understand you before. But to you Blobs in Lambda format, you might want to refer to: netlify/blobs: A TypeScript client for Netlify Blobs (github.com)

Hi again, hrishikesh,

Sorry for the delay, I’m just getting back to this after the holiday. Thanks for the pointer to the docs, that works exactly as expected!

Thanks again,
Norm