# Consume value returned from a 2nd API

**URL:** <https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 18, 2020, 9:31pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847 "2020-11-18T21:31:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelo.lucio](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@marcelo.lucio](https://community.postman.com/u/marcelo.lucio)\
**Post date:** [November 18, 2020, 9:31pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847/1 "2020-11-18T21:31:32Z")

</div>

Guys, good afternoon

I am starting to use postman and I am finding it very difficult to create some scripts, if anyone can help me, I will be very grateful;

I need to take a value that returns from an API (API 1), and compare it with the value that I am contracting on (API 2)

Example

**1ª API (make an appointment to check the available limit)**

{ “id”: “e65c4100-8974”,  
“date”: “2020-10-08T19:18:11.18Z”,  
“Values”: [ { “productId”: “3b045daf-9bda-41b0-b3ff-a64a5180c2dd”,  
“ValueDisponibile”: 1.000, }

**2 API (Contract a limit \< that the value available in api 1)**

{ “productId”: “f4ad1fba-9bfd-47d5-a0d3-f07259d8f24d”,  
“ContractedValue”: 800,}

Sorry my inglish!!!

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 18, 2020, 11:56pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847/2 "2020-11-18T23:56:01Z")

</div>

Hello @marcelo.lucio 👋

So based on the response of the API’s you have posted above, I believe “productID” is the field you need to compare from the request1 and request2, that they are matching?

Also if possible please share the complete response of the API’s here, in raw format.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [November 19, 2020, 12:25pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847/3 "2020-11-19T12:25:36Z")

</div>

In the **tests tab** of your first api you can add the following script:

```auto
const jsonData = pm.response.json();
pm.collectionVariables.set('values', JSON.stringify(jsonData.Values));

```

This will save all of the values as a variable that you can reference in other requests.

In the **tests tab** of your second api, you can add this script:

```auto
const jsonData = pm.response.json();

const values = JSON.parse(pm.collectionVariables.get('values'));
const matchingProduct = values.find(v => v.productId == productId);

pm.test('Has matching productId', function() {
  pm.expect(matchingProduct).to.not.be.undefined;
});

```

This script is trying to find a matching product from the _values_ you saved in the first API by looking for an item with the same `productId` returned in the response of your second API.

It then runs a postman test to make sure a matching value was found.

---

<div class="post-metadata">

**Author:** ![marcelo.lucio](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@marcelo.lucio](https://community.postman.com/u/marcelo.lucio)\
**Post date:** [November 19, 2020, 6:56pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847/4 "2020-11-19T18:56:33Z")

</div>

Hi Allen,

It worked what you explained to me, thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![marcelo.lucio](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@marcelo.lucio](https://community.postman.com/u/marcelo.lucio)\
**Post date:** [November 19, 2020, 6:58pm UTC](https://community.postman.com/t/consume-value-returned-from-a-2nd-api/17847/5 "2020-11-19T18:58:29Z")

</div>

Hi Pricilla,

Thank you very much for your interest in helping, our friend below managed to help me, and everything went well.
