Cannot access Workers secret

Hi,

I’m sorry, I’m unable to retrieve the secret (environment variable) that I defined in the worker’s dashboard within the worker itself. I specify the environment variable in the code, but it seems that the worker is unable to access it. I kindly request assistance.

Best regards,
Sebastian

this is my current code. The secret is the API-Key:

addEventListener(“fetch”, (event) => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
const url = new URL(request.url);
let apiUrl = https://api.openai.com/v1/chat/completions;

// Erstellen Sie einen neuen Request und kopieren Sie einige Felder vom ursprünglichen Request
let apiRequest = new Request(apiUrl, request);
apiRequest.headers.set(“Authorization”, Bearer ${OPENAI_API_KEY}); // Verwenden Sie OPENAI_API_KEY direkt

// Führen Sie die Anfrage aus und senden Sie die Antwort zurück
let apiResponse = await fetch(apiRequest);
let response = new Response(apiResponse.body, apiResponse);

return response;
}

The names need to match

Hi, thank you for your quick replay.

Both names are matching “OPENAI_API_KEY”.