Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I use an external API in my React project?

Apologies for the duplicate question I’m sure this has been asked many times before.

I’m using React, Express, CORS, node, postgres. I would like to use the following api to retrieve live price for metals:

https://metals-api.com/

I can use my API with my test app, I would also like in addition to use the API from above

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const express = require('express');
const app = express();
const cors = require("cors");
const pool = require("./db");

//middleware
app.use(cors());
app.use(express.json());

//ROUTES

And here is one of my example routes:

//Add entry to contact table
app.post("/contact", async (req, res) => {
    try {

        const {scontact_name} = req.body;
        const newContactName = await pool.query(
            'INSERT INTO supplier_contacts (scontact_name) VALUES ($1)', [scontact_name]);

        res.json(newContactName);

    } catch (err) {
        console.error(err.message);
    }
})

So to clarify there is my local API used to communicate with the postgres db. In addition I would like to use the api linked above to get some price information, help is much appreciated!

>Solution :

Javascript has a build-in fetch method (see here)
You can also use any 3rd party libary, for example axios

You can easily call their methods in a async function and then do what ever you whish with the response.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading