Consume value returned from a 2nd API

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!!!

Hello @marcelo.lucio :wave:

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.

1 Like

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

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:

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.

1 Like

Hi Allen,

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

1 Like

Hi Pricilla,

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

1 Like