I want to run the bearer token auth API only once and then run the actual API multiple times, how can I do that?

my auth API is like::

export function authentication() {
  let payload = {
  "email":"XXXX",
  "password":"XXXX"
}

let auth = http.post(`${API_URL}/auth`, JSON.stringify(payload), {
  headers: {
      "Content-Type": "application/json"
  }});
var api_token = JSON.parse(auth.body).token

return api_token
}

And I am trying to use the below code for multiple iterations:

export let options = {
  vus: 20,
  duration: '50s'
};

API to be tested is ::

export default function() {
  // POST
  let hotContracts = http.post(`${API_URL}/v1/X`,{},
  { headers: {
   "Content-Type": "application/json",
   "Authorization": `Bearer ${api_token}`}});
  //console.log("The body is =>>>> " + X.body);

  }

APIs are working fine on their own, but I want to make sure that auth API only runs once.
Can someone please help

Hi @nitesh

Welcome to the community forums! :wave:

Probably you’re looking for the setup method. See Test life cycle for the details.

Let me know if that answers,
Cheers

1 Like

thank you @olegbespalov that helped, was able to put the auth code in the setup.