Environment Variables interface

The Environment Variables interface can be used inside Edge Functions to retrieve environment variables. You can use these variables to interact with backend systems like databases, private APIs, or any authenticated service. These variables can be managed through Azion CLI (Previous versions of azion 1.x.y).


Syntax

const apiToken = Azion.env.get('API_SERVICE_TOKEN');

Parameters

ParameterTypeDescription
keystringThe key of the variable being accessed

Note: if the key informed is incorrect, it returns undefined.

Find out more about Environment variables.


Return value

A string with the value stored at the given variable’s key, or undefined if the key doesn’t exist.


Examples

// Here the environment variables are retrieved and, later on, used
// to make the connection to the DB through a fetch request.
const dbUrl = Azion.env.get('DB_URL');
const dbKey = Azion.env.get('DB_KEY');
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
// In this example, a fetch request is sent to Supabase in order to retrieve a list of names from a pre-defined table.
// The table must be configured on the supabase platform. You can find its base URL and key there as well.
const apiUrl = `${supabaseUrl}/rest/v1/names?select=*`;
const headers = new Headers({
'Content-Type': 'application/json',
'apikey': supabaseKey,
'Authorization': `Bearer ${supabaseKey}`
});
try {
const response = await fetch(apiUrl, { headers });
if (!response.ok) {
throw new Error(String(response.status));
}
const data = await response.json();
const responseBody = { data };
return new Response(JSON.stringify(responseBody), {
headers: {
'Content-Type': 'application/json'
}
});
} catch (error) {
console.error('Error connecting to Supabase: ', error);
return new Response(`Error connecting to Supabase: ${error}`, {
status: 500
});
}
}

Contributors