Jigsaw: How to access Netlify Environment Variables during build

Created on 5 Jun 2020  路  2Comments  路  Source: tighten/jigsaw

Hi All,

I realize this might not be specific to Jigsaw but does anyone know how to access Netlify Environment Variables during a production build?

I have a Listener that needs to make an API request which requires a key that I can't add to the netlify.toml file (like this):

namespace App\Listeners;

use TightenCo\Jigsaw\Jigsaw;

class GetData
{
    public function handle(Jigsaw $jigsaw)
    {
        $ch = curl_init('https://my-endpoint.com');

        curl_setopt_array($ch, [
            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
            CURLOPT_POSTFIELDS => json_encode([
                'api_key' => ____GET_API_KEY_HERE____,
            ])
        ]);

        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response);
    }
}

How can I access the Netlify Environment Variable here it says ____GET_API_KEY_HERE____?

Thanks!
Eric

question

All 2 comments

You should be able to use Laravel's env() helper, which is available in Jigsaw.

Can you try just env('API_KEY') (or whatever your env variable is) and let us know if that works?

OMG, that was easy! Thanks! I was searching everywhere for a solution.

Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattstauffer picture mattstauffer  路  3Comments

GenieTim picture GenieTim  路  4Comments

pablood85 picture pablood85  路  8Comments

knorthfield picture knorthfield  路  3Comments

lxqueen picture lxqueen  路  7Comments