My env variables are not working in production (even while using PAYLOAD_PUBLIC_ prefix)

default discord avatar
ignism11 months ago

So I want to hook up the preview button with a


variable. I looked around here for similar issues, and all seem to be solved by using the


prefix, which I already am using.

admin: {
   preview: (doc) => {
      const page = doc as any as Home;
      let pathToUse = '';
      if ( {
        pathToUse = === 'home' ? '' :;

      return `${process.env.PAYLOAD_PUBLIC_WEB_URL}/api/preview? url=${process.env.PAYLOAD_PUBLIC_WEB_URL}/${pathToUse}`;

This works in development mode, but gives me an


for each variable after building it.

  • default discord avatar
    derosul11 months ago

    I'm pretty sure the


    env variables are being replaced when you build payload (probably somewhere in your CI pipeline) Perhaps you need to define the env variables you want to use for production in there as well 🙂

  • default discord avatar
    ignism11 months ago

    When I add


    it appears to be working

    it just feels like this is not what the docs are trying me to tell

  • discord user avatar
    11 months ago

    check out this thread:

    Can you point me to the docs that confused you? Maybe we need to clean a section up

  • default discord avatar
    ignism11 months ago

    Actually this reply sums it nice up:

    I guess I was looking at it the wrong way, I just looked at it as one build. But there is of course a separation in server and client build.

  • default discord avatar
    benjick.7 months ago

    It's not really clear from this imo:

    had to find this thread

    If you're using Docker you will need to provide args for the build step and modify the Dockerfile

  • default discord avatar
    augdustlast month

    do you have a example of integrating env vars in Dockerfile?

  • default discord avatar
    benjick.last month

    @August sure

    ARG PAYLOAD_PUBLIC_MERLIN_URL=http://localhost:3000

    and then supply the arg during build time

Star on GitHub


Chat on Discord



Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.