Unauthorized error from querying tutorial FaundaDB database

Hey everyone, to preface I’m getting an unauthorized bug that I’ve seen in a few other posts in this forum, but the solutions posted in those posts haven’t helped fix the issue.

This is all the code that’s running to access my FaunaDB which is created with demo data and the Region Group is United States.

import faunadb from 'faunadb';

/* configure faunaDB Client with our secret */
const q = faunadb.query;
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET,
  domain: 'db.us.fauna.com',
  scheme: 'https',
});

exports.handler = (event, context, callback) => {
  //   return callback(null, {
  //     statusCode: 200,
  //     body: JSON.stringify('testing testing'),
  //   });
  return client
    .query(q.Get(q.Ref(q.Collection('customers'), '101')))
    .then((response) => {
      console.log('success', response);
      return callback(null, {
        statusCode: 200,
        body: JSON.stringify(response),
      });
    })
    .catch((error) => {
      console.log('error', error);
      return callback(null, {
        statusCode: 400,
        body: JSON.stringify(error),
      });
    });
};

The resulting error message I get is:

Request from 127.0.0.1: POST /.netlify/functions/todos-create
error [Unauthorized: unauthorized] {
  description: 'Unauthorized',
  requestResult: r {
    method: 'POST',
    path: '',
    query: null,
    requestRaw: '{"get":{"ref":{"collection":"customers"},"id":"101"}}',
    requestContent: u { raw: [Object] },
    responseRaw: '{"errors":[{"code":"unauthorized","description":"Unauthorized"}]}',
    responseContent: { errors: [Array] },
    statusCode: 401,
    responseHeaders: [Object: null prototype] {
      ':status': 401,
      'www-authenticate': 'Basic realm="Unauthorized"',
      'x-txn-time': '1636689123292666',
      'x-faunadb-build': '211110.020048-b14d8b0',
      'content-length': '65',
      'content-type': 'application/json;charset=utf-8',
      [Symbol(nodejs.http2.sensitiveHeaders)]: []
    },
    startTime: 1636689123004,
    endTime: 1636689123274
  }
}
Response with status 400 in 308 ms.
/Users/arshaan/Desktop/development/crop-farming/node_modules/netlify-lambda/lib/serve.js:35
    response.statusCode = lambdaResponse.statusCode;

Note: In the security tab I created a new key for the database and it has admin permissions, it is saved as in .env.development.local.

FAUNADB_SECRET=XXX123XXX

I fixed it, if anyone’s curious, make sure your process.env.FAUNADB_SECRET value is actually what you’re expecting, mine was undefined for a couple of reasons.

Hi @ArshaanB and welcome!

Very glad that to know you worked it out :smiley:

As an aside:

It looks like you may be using AWS Lambda or some service (Netlify, Vercel, etc.) that is backed by similar serverless functions. We recommend initializing your Fauna Client within the handler function, so that you always have a fresh client. This is important, as otherwise you may run into problems with how Lambda operates. See our docs for known issues. All Fauna connections are light-weight HTTP connections, with HTTP KeepAlive enabled by default, so there is no performance advantage to creating client connection objects outside of your handlers.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.