Best answer

Custom Auth Header

  • 3 December 2023
  • 1 reply



I am using the CLI to create a custom call to our REST API. I need to add a custom Authorization header value to include in the request. The calculation to define the Auth value require the URL being called along with some token data. My issue is the lack of availability of the url in the includeApiKey function where it would be needed to create the Auth header. I thought perhaps I could just create the Auth header in the test const but unfortunately the request object is not available there for me to add it. Looks like the includeApiKey fires before the test const, so not sure how I can get the url in that function. For now I am using the sample code added via the tutorial. Any suggestions how I can accomplish this? Any help appreciated.


Here’s the code snippet:

const test = (z, bundle) =>
  // The request object here might work as I can just add my custom Authorization header as part of the outbound call here.
  z.request({ url: '' });


const includeApiKey = (request, z, bundle) => {
    // The url here would be ideal as I can generate the Authorization header using standard functionality, but this is called prior to the test const above, so it's not yet available here.

  if (bundle.authData.apiKey) {    
    // Use these lines to include the API key in the querystring
    //request.params = request.params || {};
    //request.params.api_key = bundle.authData.apiKey;

    // If you want to include the API key in the header instead, uncomment this:
    request.headers.Authorization = bundle.authData.apiKey;

  return request;


Best answer by TavDev 4 December 2023, 01:22

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply


Think I found the answer. Didn’t see the Options param in the z.request function. Seems I can pass header data as part of the z.request call.


const response = await z.request({
  url: '',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'

  // only provide body, json or form...
  body: {hello: 'world'}, // or '{"hello": "world"}' or 'hello=world'
  json: {hello: 'world'},
  form: {hello: 'world'},
  // access node-fetch style response.body
  raw: false,
  redirect: 'follow',
  follow: 20,
  compress: true,
  agent: null,
  timeout: 0,
  size: 0,