DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Scrutinizing External APIs in React: How I Integrated Real-Time Dollar Data

Scrutinizing External APIs in React: How I Integrated Real-Time Dollar Data
Scrutinizing External APIs in React: How I Integrated Real-Time Dollar Data

May 7, 2025

In today’s world, building dynamic applications that provide real-time data is more important than ever. With the right approach, you can make your apps not only interactive but also insightful for users by pulling in live data from external sources. One such integration that I recently worked on was fetching and displaying real-time dollar exchange rates through an external API in a React app.


Need Help Adding External API Data to Your App?

Are you looking to integrate external data into your React or other applications? I specialize in fetching real-time data from APIs and incorporating it seamlessly into your app. Whether it’s financial data, weather information, or any other type of dynamic content, I can help you bring your app to life with real-time data integrations.

👉 Contact me today to discuss how we can elevate your app with real-time data from external APIs! https://rubystacknews.com/get-in-touch/


The Power of External APIs

External APIs open up endless possibilities for developers to enhance applications by providing access to data that would otherwise be difficult or time-consuming to collect manually. They allow you to integrate real-time data directly into your application, making it more relevant and engaging for users.

For example, when I was tasked with integrating exchange rate information into my React project, I turned to an API that provides real-time data on dollar prices. In just a few steps, I was able to bring valuable financial data into the app.

How I Scrutinized and Integrated the API

The integration process began with fetching data from a public API endpoint: https://criptoya.com/api/dolar. The goal was to display information like the official dollar price, variation, and timestamp for both the “blue” and official exchange rates.

Here’s the simplified version of the logic I implemented:


import React, { useEffect, useState } from 'react';
import { useTranslation } from "react-i18next";

function Dollars() {
  const { t } = useTranslation();
  const [dollars, setDollars] = useState({});
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch('https://criptoya.com/api/dolar')
      .then(response => response.json())
      .then(data => {
        setDollars(data);
        setLoading(false);
      })
      .catch(error => {
        console.error('Error fetching dollars', error);
        setError('Failed to fetch data');
        setLoading(false);
      });
  }, []);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>{error}</p>;

  return (
    <>
      <div style={blockStyle}>
        <h2>{t('blue_dollar')}</h2>
        <p>{t('ask')}: {dollars.blue.ask}</p>
        <p>{t('bid')}: {dollars.blue.bid}</p>
        <p>{t('variations')}: {dollars.blue.variation}</p>
        <p>{t('timestamp')}: {new Date(dollars.blue.timestamp).toLocaleString()}</p>
      </div>
      <div style={blockStyle}>
        <h2>{t('official_dollar')}</h2>
        <ul>
          <li>{t('higher_price')}: {dollars.oficial.highestPrice}</li>
          <li>{t('lower_price')}: {dollars.oficial.lowestPrice}</li>
          <li>{t('variations')}: {dollars.oficial.variation}</li>
          <li>{t('timestamp')}: {new Date(dollars.oficial.timestamp).toLocaleString()}</li>
        </ul>
      </div>
    </>
  );
}

const blockStyle = {
  backgroundColor: '#f0f0f0',
  padding: '20px',
  borderRadius: '8px',
  boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)',
  marginBottom: '20px',
};

export default Dollars;

Enter fullscreen mode Exit fullscreen mode

Key Challenges I Encountered

Article content

  1. Data Structure : The API returned the dollar data in a complex structure, including various properties like ask, bid, variation, and timestamp. I had to carefully extract the relevant fields and display them in an intuitive way for users.
  2. Error Handling : Working with external APIs comes with the risk of data inconsistencies or network failures. I made sure to handle errors gracefully, providing a meaningful message if something went wrong.
  3. Real-Time Data : The nature of exchange rate data is that it changes constantly. The app was designed to fetch the most up-to-date information and display it in real time.

Best Practices for API Integration

Article content

  1. Error Handling : Always ensure that your app gracefully handles failures, whether it’s a network issue, bad data, or unexpected API behavior.
  2. Caching Data : Depending on your use case, you might want to cache the API response to reduce unnecessary calls to the server.
  3. User Feedback : Make sure that your app informs users when data is loading or if something goes wrong. For example, a spinner or loading message can be displayed while the data is being fetched.
  4. Data Formatting : Always format the data in a user-friendly way. For example, I used toLocaleString() to format the timestamp and present it in a more readable format.

Article content

Article content


Conclusion:

Integrating an external API into your React application can enhance its functionality and provide real-time insights to users. By following best practices such as proper error handling and displaying data in a user-friendly manner, you can deliver a robust and dynamic experience. Whether you’re displaying exchange rates, weather data, or social media feeds, APIs are a powerful tool to enrich your React apps.

If you’re interested in more insights on working with APIs or React in general, feel free to reach out or connect with me on LinkedIn!

Article content


Hashtags (Optional):

ReactJS #WebDevelopment #APIIntegration #JavaScript #Tech #FrontendDevelopment #ExternalAPI #ReactNative #WebApps

Top comments (0)