Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Error: missing secret key. A secret key is needed to secure Payload.

default discord avatar
notchr7 months ago
7

Anytime!

  • default discord avatar
    creonico8 months ago

    Alright, thank you again

  • default discord avatar
    notchr8 months ago

    If you do find out, let me know for sure, im glad it works though 😄



    That's the extent of my knowledge though, not sure if there's some process that pulls it in the first time



    Otherwise they are pulled in via an .env file and the dotenv library



    And im not sure, typically environment variables are only added if they are declared as part of a command or manually declared



    Good question, not the first time I've seen it too



    Hmm

  • default discord avatar
    creonico8 months ago

    It was working yesterday without dotenv/config



    it works now, thank you. But may I know why it failed before?

  • default discord avatar
    notchr8 months ago

    Then try logging again



    at the top



    import 'dotenv/config'


    can you try to add



    Okay lets try to fix that



    Ah

  • default discord avatar
    creonico8 months ago

    It prints but it does not contain the variables in my .env

  • default discord avatar
    notchr8 months ago

    Try logging it prior to the buildConfig



    And when doing so, is it an object with the expected variables



    For instance, can you log the value of your process.env



    @254395642565099530

    Is your .env being picked up correctly?

  • default discord avatar
    creonico8 months ago

    This was working yesterday in sveltekit, but today if fails with this error after I npm run dev:


    Error: missing secret key. A secret key is needed to secure Payload.

    But my config has the secret PAYLOAD_SECRET property from my .env file:


    import path from 'path'
    import { buildConfig } from 'payload'
    import { fileURLToPath } from 'url'
    import { postgresAdapter } from '@payloadcms/db-postgres'
    
    import { Products } from './collections/Products'
    
    const filename = fileURLToPath(import.meta.url)
    const dirname = path.dirname(filename)
    
    export default buildConfig({
      collections: [Products],
      secret: process.env.PAYLOAD_SECRET || '',
      typescript: {
        outputFile: path.resolve(dirname, 'payload-types.ts'),
      },
      db: postgresAdapter({
        pool: {
          connectionString: process.env.DATABASE_URI || '',
        },
      })
    })

    I then use the local API as such, this is where the error comes from but it was working yesterday


    import config from '../../payload.config'
    import { getPayload } from 'payload';
    
    const payload = await getPayload({ config });
    
    export async function load() {
      const data = await payload.findByID({
        collection: 'products',
        id: 1
      });
    
      return data;
    }

    I have not changed my package.json at all, in fact, I only ran the command since yesterday and now it gives me this error. I am using payload 3.0.0-beta.74

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.