Can we set and use collection variables in K6?

So I have set a collection variable as follows:

  function accessTokenRequest() {
    let response =, payload, { headers: headers });
    let responseBody = JSON.parse(response.body);
      `Bearer ${responseBody.access_token}`

In a seperate login script that I use. Then I use this variable in my K6 script as the value for the Authorization header:

Authorization: "{{access_token}}",

When I start running my script however, I get the following error:
ERRO[0005] Error: May only be used in a request scope
And the pm.collectionVariables.set() function is the one referred to. Which would imply we cannot set collection variables from the responsebody and very problematic for our rollout.

The request itself does return a 200.