Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How do you pull specific data from an external API using axios

So I have an external API that I would like to filter out. Part of the API shows this information (in JSON format as an example:

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
}

Now I am using axios to call all this data. This is the code:

//endpoint that will fetch data from an external API
app.get("/externalapi/", (req, res, next) => {
    let apiURL = 'https://test.herokuapp.com/api/v1/data';
   
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

However, I would like to filter it out to where a specific endpoint would show me only Fiona's information and only show me Fiona's first name, last name, and phone number. I tried filtering it out using this endpoint

app.get("/externalapi/:first_name", (req, res, next) => {

However, it's only giving me errors when I test it out on Postman. What am I missing?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Can write a general function that will take the array and the name of the person you want to return:

const data = {
    data: [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        ]
}

const getData = (arr, name) => {
  const nameData = arr.filter(el => (el.first_name === name))
  const returnedObj = nameData[0]
  return returnedObj
}

const filteredData = getData(data.data, "Fiona")

const {first_name, last_name, phone_number} = filteredData
console.log(first_name)
console.log(last_name)
console.log(phone_number)

about 4 years ago · Juan Pablo Isaza Report

0

you can try filtering out the response data using the first name

const userData = response.data.filter((selection)=>{ 
return selection.first_name === 'Fiona';

});

console.log(userData);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!