How to add a variable to a header in an external API request function

Options
I'm trying to add variables in headers for an external request. The header of these API requests are written like this Authorization: Bearer (API Key or token). However, in this case, the API key is a variable but when I try put in the variable from the list of variables it clears out the other parts of the text i.e Authorization: Bearer

Can I please get help on how to go about this?

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Hi  I recommend using sprintf or replace filter to accomplish variable substitution. Here's how to use both:

    
  • David Richardson
    Options
    There is a process for this.  Ray Deck has gone through this process at State Change ProI trust you have a cURL from a third party and Xano does a nice job setting that up.  [Xano cURL 01.jpg]Best place to start is to create a new API and "Start from scratch" where in the first screen (after that) you have the option to add an external API request
    [Xano cURL 02.jpg][Xano cURL 03.jpg]
    click the import CURL button[Xano cURL 04.jpg]that exposes this screen to paste your CURL.   
    [Xano cURL 05.jpg]I got the following CURL (below) from Webflow API documentation but they might all be similar in concept.  After pasting the CURL select "import" button.  Take note of the method and select the right verb: GET, POST, PATCH, DELETE... 

    And then address assigning the values from your function stack to the various params.  Typically params are an object so you will need to set the value from data obtained or created  earlier in YOUR function stack.  That is beyond the scope of this message.
    [Xano cURL 06.jpg]
    After you do the params, lower on the page Xano maps out additional input for you including the headers shown below.  You'll have to scroll down the page to show them.  Take note of the red boxed input for {access token}
    [Xano cURL 07.jpg]This next bit requires some preparation I might have mentioned earlier, but it wouldn't have made sense then.  The best course of action is to create an "environment variable" to allow you to set up an API key value in a secure location in Xano and use it in all related API calls.  It's easier to mange this way.

    Head over to the gear icon (see below) boxed in red
    [Xano cURL 08.jpg] Clicking the gear icon exposes the General settings page.  Click the "manage" button that allows you to create name:value pairs for environment variables.  All caps with underscores is the fashion.[Xano cURL 09.jpg]I want to mention a gotcha when changing the value of an environment variable.  The trash can icon (on the right side of the input box) deletes THE WHOLE ENVIRONMENT VARIABLE!!!  ARGH.  If you just want to change the value inside the box, hover over the box and that exposes an "x" on the right.  Clicking the "x" will clear all the text in the value box.

    Ray Deck recommends the following procedure to set the values of the headers in your CURL.  He likes to use the "replace" filter to avoid strange things happening with text values in computer memory.  Where you see {access_token} (above), replace the whole string including the curly braces with the name of the environment variable prepended with a dollar sign. So the WEBFLOW_APIKEY for these header parameters -  {access_token} in this example becomes $WEBFLOW_APIKEY.   $WEBFLOW_APIKEY is just a string.  It might be "foobar" but to keep things clear use naming conventions to make things clear.  $WEBBFLOW_APIKEY or YOUR environment variable will reference the ENV variable.  with the "$" it's just a proxy "value" string.

    The next step is to use the "replace" filter to replace the proxy value with the actual value of your environment variable.[Xano cURL 10.jpg]For example, you would enter the text: $WEBFLOW_APIKEY in the text to replace box, with the ENV variable WEBFLOW_APIKEY.  That's the environment variable you just entered - the one YOU need in your CURL request, not what I entered above - use the one you need.  That will create the header you need to make your CURL work.

    Try it out with "run and debug"

    Good luck.
  • Ambroise
    Ambroise Member
    Options

    I couldn't understand either answers because all images are broken.

    Could someone address the basics? What is the format expected by `headers`? I tried providing an object but got `Text filter requires a scalar value.`. I tried providing a string but it didn't work, either.

    The expected format should be specified in the UI, it's not normal than developers cannot figure this out on their own.

  • Ambroise
    Ambroise Member
    Options

    Well, it seems Xano is expecting an Array of strings. That's not intuitive at all.

    I'm still getting 401, though. But the provided headers seem to be of the right format, keeping on digging.

  • Ambroise
    Ambroise Member
    Options

    I was initially trying to call another Xano endpoint (from the same project), but couldn't find how to do that, so I used External API Request, and I've been struggling for 30mn+ just to pass the headers.

    Doesn't seem to be working, even if the `Authorization` seems to be right.