Real-time crypto updates

Hello,

I created my website with lovable.dev and I don’t know how to make it display the real-time cryptocurrency price because it’s currently hard-coded.
Any ideas?

import React, { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import Navbar from "@/components/Navbar";
import Footer from "@/components/Footer";
import CryptoCard from "@/components/CryptoCard";
import ServiceCard from "@/components/ServiceCard";
import PortfolioSimulator from "@/components/PortfolioSimulator";
import TestimonialCard from "@/components/TestimonialCard";
import ContactSection from "@/components/ContactSection";
import { useToast } from "@/components/ui/use-toast";
import { Bitcoin, TrendingUp, Shield, Users, Wallet, ChartBar } from "lucide-react";
const Index: React.FC = () => {
  const {
    toast
  } = useToast();
  const [cryptoData, setCryptoData] = useState([{
    id: "bitcoin",
    name: "Bitcoin",
    symbol: "BTC",
    price: 56000,
    change24h: 2.5,
    volume: 32000000000,
    marketCap: 1100000000000
  }, {
    id: "ethereum",
    name: "Ethereum",
    symbol: "ETH",
    price: 3200,
    change24h: 3.2,
    volume: 18000000000,
    marketCap: 380000000000
  }, {
    id: "binancecoin",
    name: "Binance Coin",
    symbol: "BNB",
    price: 360,
    change24h: -1.2,
    volume: 2100000000,
    marketCap: 60000000000
  }, {
    id: "solana",
    name: "Solana",
    symbol: "SOL",
    price: 110,
    change24h: 5.8,
    volume: 3500000000,
    marketCap: 46000000000
  }]);

Find a library or an API that will give you the data, insert the data?

1 Like

Yes, for example, I’d like to use the Google console to find an API to provide real-time crypto rates and link it to lovable.dev, but I don’t see any in the Google console search.
Do you have any ideas?

Search for crypto market data api. You’ll find plenty.

1 Like

I tried using the Coincap API for crypto data, but I’m not receiving any data, even though I specified in lovable.dev to use the CoinCap API.

Do you have any ideas as to what the problem is?

From the information you’ve give so far, no. No idea. Can you describe some more what you’ve done, show some code, etc?

Also, I can’t read French.

1 Like

Actually, I’m doing it from lovable.dev.

And I’m asking it to use the CoinCap API, and it doesn’t work.

I changed it and specified the correct link with my API key and it still doesn’t work.
'https://api.coincap.io/v2' change to const API_BASE_URL = 'rest.coincap.io/v3/assets?apiKey=f1d4a2d19e0e8dd20000434b685176b2ee5ab7xxxxxxxxxxx';

Here’s the code coinCapServices.ts

// CoinCap API service for fetching cryptocurrency data
export interface CryptoAsset {
  id: string;
  rank: string;
  symbol: string;
  name: string;
  priceUsd: string;
  changePercent24Hr: string;
  marketCapUsd: string;
  volumeUsd24Hr: string;
}

export interface CryptoMarketData {
  assets: CryptoAsset[];
  loading: boolean;
  error: string | null;
}

// Base URL for the CoinCap API
const API_BASE_URL = 'rest.coincap.io/v3/assets?apiKey=f1d4a2d19e0e8dd20000434b685176b2ee5ab7xxxxxxxxxxx';

// Function to fetch the top cryptocurrencies
export async function fetchTopCryptos(limit: number = 10): Promise<CryptoAsset[]> {
  try {
    const response = await fetch(`${API_BASE_URL}/assets?limit=${limit}`);
    
    if (!response.ok) {
      throw new Error('Failed to fetch crypto data');
    }

    const data = await response.json();
    return data.data;
  } catch (error) {
    console.error('Error fetching crypto data:', error);
    throw error;
  }
}

// Function to fetch data for a specific cryptocurrency
export async function fetchCryptoDetails(id: string): Promise<CryptoAsset> {
  try {
    const response = await fetch(`${API_BASE_URL}/assets/${id}`);
    
    if (!response.ok) {
      throw new Error(`Failed to fetch data for ${id}`);
    }

    const data = await response.json();
    return data.data;
  } catch (error) {
    console.error(`Error fetching data for ${id}:`, error);
    throw error;
  }
}

// Function to fetch historical data for a specific cryptocurrency
export async function fetchCryptoHistory(
  id: string, 
  interval: string = 'd1',
  start?: number,
  end?: number
): Promise<any> {
  try {
    let url = `${API_BASE_URL}/assets/${id}/history?interval=${interval}`;
    
    if (start) url += `&start=${start}`;
    if (end) url += `&end=${end}`;

    const response = await fetch(url);
    
    if (!response.ok) {
      throw new Error(`Failed to fetch history for ${id}`);
    }

    const data = await response.json();
    return data.data;
  } catch (error) {
    console.error(`Error fetching history for ${id}:`, error);
    throw error;
  }
}

// Format price in EUR
export function formatPriceEUR(priceUsd: string): string {
  const price = parseFloat(priceUsd);
  return new Intl.NumberFormat('fr-BE', { 
    style: 'currency', 
    currency: 'EUR',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2 
  }).format(price * 0.92); // Approximate USD to EUR conversion
}

// Format large numbers with Belgian number formatting
export function formatLargeNumber(value: string): string {
  const num = parseFloat(value);
  return new Intl.NumberFormat('fr-BE').format(num);
}

// Format percentage change
export function formatPercentageChange(change: string): string {
  const changeNum = parseFloat(change);
  const formattedChange = changeNum.toFixed(2);
  return changeNum >= 0 ? `+${formattedChange}%` : `${formattedChange}%`;
}

So…
#1 you’re using code designed for v2 of the API, on the v3 version of it?
#2: you removed the protocol from the url.
#3:

Think about what your URL looks like if you insert that variable into the string. Can we see why it may not be happy?

Thank you for your reply.

And what do I need to do in the code to make it work?

I’m not going to write your code for you.

If you dont see the problem with what you’re doing, you’re either in too deep, or not trying.

If it’s the former, hire someone who knows what they’re doing.

If it’s the latter, I shall say goodday to you.

1 Like

I don’t know how to do it because I’m building my website with the AI ​​lovable.dev and it writes the code for me.
And I don’t see what I need to change. I just tested this line alone in Chrome and it works, but I don’t know how to implement it in the code.

rest.coincap.io/v3/assets?apiKey=f1d4a2d19e0e8dd20000434b685176b2ee5ab7xxxxxxxxxxx

So you had the AI write the code, didnt understand what it wrote, and then modified what it wrote yourself, and it broke.

Do… you maybe think you should ask the AI to make the change for you instead? Maybe it’s smart enough to understand why the URL has to be written as… yaknow… a proper url.

I’ll give you a hint.

'rest.coincap.io/v3/assets?apiKey=f1d4a2d19e0e8dd20000434b685176b2ee5ab7xxxxxxxxxxx'

It worked with the first string. Dont you think you should make the second string look like the first string?

Try like this:

// Base URL for the CoinCap API
const API_BASE_URL = 'https://rest.coincap.io/v3/assets?apiKey=f1d4a2d19e0e8dd20000434b685176b2ee5ab7xxxxxxxxxxx';

const response = await fetch(`${API_BASE_URL}&limit=${limit}`);

Have your web browser tool opened to see what is coming back from the server.
I get 403 Forbidden because I don’t have a real key, but yous should show a nice json files with 10 values.

1 Like

Great, thank you, I did it like that and it works.

const API_KEY = 'f1d4a2d19e0e8dd20000434b685176b2ee5abXXXXXXX';
const API_BASE_URL = 'https://rest.coincap.io/v3/assets';

// Function to fetch the top cryptocurrencies
export async function fetchTopCryptos(limit: number = 10): Promise<CryptoAsset[]> {
  try {
    const response = await fetch(`${API_BASE_URL}?apiKey=${API_KEY}&limit=${limit}`);
    
    if (!response.ok) {
      throw new Error('Failed to fetch crypto data');
    }

Do you know of a completely free crypto API?

No idea. I doubt there any one free out here.

One trick you can do is create your own API, that will take data from some saved source. This source will be updated let said 5 times a day (do you own math here) from CoinCap free tier so you don’t hit the free limit.

Free Tier

  • 2,500 credits per month

in fact I did the update every 60 seconds and I am already above the free plan, I will change it to do an update according to my free plan of 2500 crédits.

Hello,

I created a free account and an API key. I tried as described in the documentation, but it doesn’t work. Do you have any ideas?

in the documentation they say to just remove “pro-”

https://pro-api.coingecko.com/api/v3/ping?x_cg_pro_api_key=YOUR_API_KEY
https://api.coingecko.com/api/v3/ping?x_cg_pro_api_key=CG-58gZtit85qm6XXXXXXXX

Are you certain that you don’t need to remove “pro” from the parameter name as well as the URL?

2 Likes

Yes, that’s what the guide says.
https://docs.coingecko.com/reference/setting-up-your-api-key