# API Query using Variable

**URL:** <https://matillioncommunity.discourse.group/t/api-query-using-variable/2310>\
**Category:** Matillion ETL\
**Created:** [October 12, 2023, 5:01am UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310 "2023-10-12T05:01:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrickS](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)\
**Post date:** [October 12, 2023, 5:01am UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/1 "2023-10-12T05:01:15Z")

</div>

I have a mapping as below:

API Query0 -\> Table query to Grid - \> Api Query1

&nbsp;

In API Query0 - I get a daily changing authentication bearer token from API and load it into a table.

In Query Result To Grid - I load the authentication bearer token from table to a grid variable called $token.

In API Query1 - I am trying to use the $token from grid variable by inputting in the RSD file header parameter as Bearer [\_input.token] so that Bearer token gets populated automatically at runtime but i am getting below error:

&nbsp;

"**Exception testing table. Formatter [token] failed in the evaluation of Bearer [\_input.token]. The error was: The value of the attribute could not be accessed: The attribute does not exist.**"

&nbsp;

What am i doing wrong here?

 ![0694G00000UsM9KQAV_0D54G00009gT2v9SAC](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/c5b1d61eb3e71870e14453891bda61eed6e1b8e9.png)

 ![0694G00000UsM8qQAF_0D54G00009gT2v9SAC](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/33c1712a8e6e185466c8d939b99b998f64f6a61e.png)

 ![0694G00000UsM9oQAF_0D54G00009gT2v9SAC](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/85508d808a97cb466749b61fdd9abb3469aa0819.png)

---

<div class="post-metadata">

**Author:** ![PatrickS](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)\
**Post date:** [October 18, 2023, 3:25am UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/2 "2023-10-18T03:25:40Z")

</div>

[@BenSmilesMatillionProductOwner](https://matillioncommunity.discourse.group/u/BenSmilesMatillionProductOwner)​ - Hello Ben, I had asked you a question a few months back and you had helped. Can you please help me with this question about Grid or Job variable in API Query. Thanks.

---

<div class="post-metadata">

**Author:** ![ramalingam](https://sea2.discourse-cdn.com/flex002/user_avatar/matillioncommunity.discourse.group/ramalingam/32/909_2.png) [@ramalingam](https://matillioncommunity.discourse.group/u/ramalingam)\
**Post date:** [October 20, 2023, 6:08am UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/3 "2023-10-20T06:08:57Z")

</div>

[@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)​ - you can use the CustomHeaders attribute in the Connection Options window to set this - rather than using the Authorization header in the RSD.

Find more details here.

https://docs.matillion.com/metl/docs/obtaining-api-token-and-passing-to-api-query-profile/#1.7

Also you probably want to use Query Result to Scalar component to extract the token from the db and assign it to a job variable that you can then send it as a CustomHeader.

---

<div class="post-metadata">

**Author:** ![ramalingam](https://sea2.discourse-cdn.com/flex002/user_avatar/matillioncommunity.discourse.group/ramalingam/32/909_2.png) [@ramalingam](https://matillioncommunity.discourse.group/u/ramalingam)\
**Post date:** [October 25, 2023, 8:18am UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/4 "2023-10-25T08:18:50Z")

</div>

[@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)​ - if you are getting 401 Unauthorized, then it means one of the below few cases.

- The bearer token is invalid. Can you check if the token works when invoking the API from outside Matillion - say in Postman
- The CustomHeaders value is not being sent to the API endpoint. Enable Auto Debug in the API Query component and check the logs when you run the job. https://docs.matillion.com/metl/docs/2338785/
- Can you ensure that you have Authentication Method in the API Query component to "Other" and not "Bearer token".

Hope this helps.

---

<div class="post-metadata">

**Author:** ![PatrickS](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)\
**Post date:** [October 24, 2023, 9:15pm UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/5 "2023-10-24T21:15:41Z")

</div>

[@ramalingam](https://matillioncommunity.discourse.group/u/ramalingam)​ - Hello I have followed the link you sent me as below:

1) Set the job variable as $token

 ![0694G00000UtYc6QAF_0D74G000008B5PeSAK](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/c60e709c9365fa65c973cd29830bd822fe84808c.png)

1. 

 ![0694G00000UtYcfQAF_0D74G000008B5PeSAK](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/45aadaa50e190b35d0d63c1379fcacadd7b7ba81.png)

1. 

 ![0694G00000UtYcpQAF_0D74G000008B5PeSAK](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/99214309477b495dffc1fed28c049c0aaad1fe64.png)

I get the message that “Query result processing to scalar successful.” which i assumed as value assigned to $token

BUT for API Query i get “HTTP protocol error. 401 Unauthorized”.

Any idea what i am doing wrong or missing here?

Thanks

Patrick.

---

<div class="post-metadata">

**Author:** ![PatrickS](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@PatrickS](https://matillioncommunity.discourse.group/u/PatrickS)\
**Post date:** [October 25, 2023, 6:07pm UTC](https://matillioncommunity.discourse.group/t/api-query-using-variable/2310/6 "2023-10-25T18:07:28Z")

</div>

It seems to be working now, the changed the job variable to be token instead of $token, I think it was main mistake. I also turned BackwardsCompatability to false and added a parameter Header with Value as connection as i saw in the forum answers, but I don't know if these do anything in my case.

&nbsp;

Thank you so much for you first suggestion as that link really helped me. Best Regards.

 ![0694G00000Utg34QAB_0D74G000008B5gQSAS](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/b431c4ac556c7d040d02f3542249c0f33c625abc.png)
