Cryptocurrency applications require real-time price data to display market values such as Bitcoin, Ethereum, and other digital assets. Many developers previously used the CoinDesk API, but it may sometimes fail due to DNS or service issues. A reliable alternative is the CoinGecko API, which provides free cryptocurrency price data without requiring authentication or an API key.
This article explains the concept of APIs, demonstrates how to consume a real-time cryptocurrency API, and provides complete backend and frontend code using ASP.NET Web API and JavaScript.
Understanding the API Concept
An API (Application Programming Interface) is a bridge that allows one software application to communicate with another. Instead of storing cryptocurrency prices locally, applications request the latest data from a remote server that maintains up-to-date market information.
In this example, the application sends an HTTP request to a public API. The API returns a JSON response containing the latest cryptocurrency price.
The workflow is simple:
User opens the webpage
Frontend sends request to ASP.NET API
ASP.NET API calls the CoinGecko API
CoinGecko returns the latest Bitcoin price
Backend returns the result to the frontend
Frontend displays the price
Recommended Free Crypto API
CoinGecko provides a free and reliable API endpoint.
API Endpoint
https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd
Sample JSON Response
{
"bitcoin": {
"usd": 68421
}
}
This response shows that the current Bitcoin price is 68421 USD.
Backend Implementation Using ASP.NET Web API
First create a Web API controller that will fetch cryptocurrency prices from CoinGecko.
CryptoController.cs
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
namespace CryptoAPI.Controllers
{
public class CryptoController : ApiController
{
[HttpGet]
[Route("api/crypto/bitcoin")]
public async Task<IHttpActionResult> GetBitcoinPrice()
{
string url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return BadRequest("Unable to fetch crypto data");
}
string result = await response.Content.ReadAsStringAsync();
return Ok(result);
}
}
}
}
Explanation of the Backend Code
The controller creates an HTTP request to the CoinGecko API using HttpClient. When the API responds, the JSON data is captured and returned to the frontend. This acts as a middleware layer between the public API and the client application.
Advantages of this approach include:
Frontend Implementation Using HTML and JavaScript
Now create a simple webpage to display the real-time Bitcoin price.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Live Crypto Price</title>
<style>
body{
font-family: Arial;
text-align:center;
margin-top:100px;
background:#f4f4f4;
}
.container{
background:white;
padding:30px;
width:350px;
margin:auto;
box-shadow:0 0 10px #ccc;
border-radius:10px;
}
.price{
font-size:28px;
color:green;
margin-top:20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Bitcoin Live Price</h1>
<div id="price" class="price">
Loading...
</div>
<button onclick="loadPrice()">Refresh Price</button>
</div>
<script>
function loadPrice()
{
fetch("/api/crypto/bitcoin")
.then(response => response.json())
.then(data => {
let obj = JSON.parse(data);
let price = obj.bitcoin.usd;
document.getElementById("price").innerHTML = "$ " + price;
})
.catch(error => {
document.getElementById("price").innerHTML = "Error loading price";
});
}
loadPrice();
</script>
</body>
</html>
How the Frontend Works
The JavaScript fetch function sends a request to the ASP.NET API endpoint. Once the response arrives, the JSON data is parsed and the Bitcoin price is displayed inside the webpage.
When the user clicks the refresh button, the API is called again and the updated price is displayed.
Alternative Free Crypto API
Another working cryptocurrency API is available at:
https://api.alternative.me/v2/ticker/bitcoin/
Example Response
{
"data": {
"1": {
"name": "Bitcoin",
"symbol": "BTC",
"quotes": {
"USD": {
"price": 68200
}
}
}
}
}
This API also returns real-time price data and can be integrated using the same HttpClient method in ASP.NET.
Testing the API
To test the API directly, open the following URL in your browser:
https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd
If the API is working correctly, the browser will return JSON data containing the current Bitcoin price.
Benefits of Using Public Crypto APIs
No database maintenance required
Always updated real-time market data
Easy integration with web and mobile apps
No authentication required for basic endpoints
Common Use Cases
Conclusion
Using a free cryptocurrency API like CoinGecko makes it easy to integrate real-time market prices into applications. By combining ASP.NET Web API for the backend and JavaScript for the frontend, developers can quickly build scalable crypto dashboards that display live data. This architecture keeps the frontend simple while allowing the backend to manage external API communication efficiently.