Unauthorized when using valid secret key


I am new to fauna, I was hoping someone with more experience could give me a hand with this issue.

I am trying to integrate fauna to a nodejs javascript application.

I have followed the docs and the examples in there, but I cannot connect to the database, I keep getting unauthorized error.

I have created a secret key with the admin role in the fauna dashboard, and tried to use it to configure my fauna client.

This is my code, I have debugged and checked that the client has the correct secret value.

import faunadb from 'faunadb'
import { Bussines } from './models'
const q = faunadb.query
const { Get, Ref, Collection } = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNA_SECRET_KEY as string,

export async function findBusinessByCode(code: string) {
  try {
      const obj = await client.query<Bussines>(
      Get(Ref(Collection('Businesses'), parseInt(code, 10)))

    return obj
  } catch (error) { 
   // Unauthorized {name: 'Unauthorized', description: 'Unauthorized'}

I checked the _http object inside client:

_adapter:Http2Adapter {type: 'http2', _sessionMap: {…}, _http2SessionIdleTime: 500, _closed: false}
_headers:{X-FaunaDB-API-Version: '4', X-Driver-Env: 'driver=javascript-4.3.0; runtime=nodejs-v14.17.3; env=unknown; os=win32-10.0.19043', Authorization: 'Bearer fnAEO-FLuTHISISTHEKEYFROMTHEDASHBOARD', X-Last-Seen-Txn: 1627211605975384, X-Query-Timeout: null}
X-Driver-Env:'driver=javascript-4.3.0; runtime=nodejs-v14.17.3; env=unknown; os=win32-10.0.19043'

I have also tried creating custom roles adding the corresponding permissions as per the tutorials with no success.

Any ideas?

Thanks so much for reading.

Hi @eltuna9 ! This is the endpoint for the “Classic” region group. Can you confirm that the relevant database is not in the US or EU Region Group? A mismatch here will always return an auth error.

If it is the mismatch with the region groups, then you need to specify the domain option to the client.

Endpoints are specified in Region Groups docs.
How to use those endpoints with your favorite driver (including Node) are in the Connections docs.

const client = new faunadb.Client({
  secret: process.env.FAUNA_SECRET_KEY,

  // US Region group, for example
  domain: "db.us.fauna.com"

[edit: domain must be a string]

1 Like

Thanks so much @ptpaterson!

That was my case as my database is in the US region. After adding the domain to the client object it worked as expected.

Just a detail, that may be obvious but may save some time to someone who has the same issue and is copy/pasting this, the domain property is a string.

const client = new faunadb.Client({
  secret: process.env.FAUNA_SECRET_KEY as string,
  domain: 'db.us.fauna.com'

I appreciate you taking some time to reply to this issue.


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