On this page
Show Current Price
Fetch the latest BTC/USDT price using a simple REST call and show it in a dashboard card.
Result
A simple dashboard card showing the real-time Bitcoin price fetched from our API.
Bitcoin Price (BTC/USDT)
Used Technologies
- HTML/CSS/JS: For the UI and logic.
- Fetch API: To make the HTTP request.
- Bitcoin API: Specifically the
getCurrentPriceendpoint.
Implementation Steps
1. The HTML Structure
Create a card structure with placeholders for the price and a refresh button.
html
<div class="price-card">
<div class="price-card__header">
<span class="price-card__label">Bitcoin Price (BTC/USDT)</span>
<div id="status-dot" class="price-card__dot"></div>
</div>
<div class="price-card__body">
<div id="loading-skeleton" class="price-card__loading">
<div class="skeleton"></div>
</div>
<div id="error-message" class="price-card__error"></div>
<div id="price-display" class="price-card__value" style="display: none;">
<span class="price-card__currency">$</span>
<span id="price-number" class="price-card__number">0.00</span>
</div>
</div>
<div class="price-card__footer">
<span class="price-card__source">Source: bitcoinapi.dev</span>
<button id="refresh-btn" class="price-card__refresh">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"></path><path d="M21 3v5h-5"></path></svg>
</button>
</div>
</div> 2. Styling the Card
Use CSS to create a modern, responsive card layout with loading animations.
css
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f8fafc;
}
.price-card {
background: white;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 1.5rem;
width: 100%;
max-width: 320px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.price-card__header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
}
.price-card__label {
font-size: 0.75rem;
font-weight: 600;
color: #64748b;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.price-card__dot {
width: 8px;
height: 8px;
background: #10b981;
border-radius: 50%;
box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.1);
}
.price-card__dot.is-loading {
background: #f59e0b;
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.4; }
100% { opacity: 1; }
}
.price-card__body {
margin-bottom: 1rem;
}
.price-card__value {
display: flex;
align-items: baseline;
color: #0f172a;
}
.price-card__currency {
font-size: 1.5rem;
font-weight: 500;
margin-right: 0.25rem;
color: #94a3b8;
}
.price-card__number {
font-size: 2.5rem;
font-weight: 800;
letter-spacing: -0.02em;
}
.price-card__footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 1rem;
border-top: 1px solid #f1f5f9;
}
.price-card__source {
font-size: 0.75rem;
color: #94a3b8;
}
.price-card__refresh {
background: transparent;
border: none;
color: #64748b;
cursor: pointer;
padding: 4px;
border-radius: 4px;
display: flex;
align-items: center;
transition: all 0.2s;
}
.price-card__refresh:hover:not(:disabled) {
background: #f1f5f9;
color: #0f172a;
}
.price-card__refresh:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.skeleton {
height: 3rem;
background: #f1f5f9;
border-radius: 8px;
animation: skeleton-pulse 1.5s infinite;
}
@keyframes skeleton-pulse {
0% { background-color: #f1f5f9; }
50% { background-color: #e2e8f0; }
100% { background-color: #f1f5f9; }
}
.price-card__error {
color: #ef4444;
font-size: 0.875rem;
display: none;
} 3. Implementing the Logic
Use JavaScript to fetch the price from the API and update the DOM elements.
javascript
const BASE_URL = import.meta.env.VITE_API_BROWSER_URL || 'https://bitcoin-api.net/api';
const API_KEY = import.meta.env.VITE_BITCOIN_API_KEY;
const priceNumber = document.getElementById('price-number');
const priceDisplay = document.getElementById('price-display');
const loadingSkeleton = document.getElementById('loading-skeleton');
const errorMessage = document.getElementById('error-message');
const statusDot = document.getElementById('status-dot');
const refreshBtn = document.getElementById('refresh-btn');
async function fetchPrice() {
try {
// Show loading state
loadingSkeleton.style.display = 'block';
priceDisplay.style.display = 'none';
errorMessage.style.display = 'none';
statusDot.classList.add('is-loading');
refreshBtn.disabled = true;
const headers = {};
if (API_KEY) {
headers['Authorization'] = `Bearer ${API_KEY}`;
}
const response = await fetch(`${BASE_URL}/v1/prices/current?symbol=btcusdt`, { headers });
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || data.code || 'Failed to fetch price');
}
// Update UI
priceNumber.textContent = parseFloat(data.price).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
loadingSkeleton.style.display = 'none';
priceDisplay.style.display = 'flex';
} catch (err) {
console.error(err);
errorMessage.textContent = err.message;
loadingSkeleton.style.display = 'none';
errorMessage.style.display = 'block';
} finally {
statusDot.classList.remove('is-loading');
refreshBtn.disabled = false;
}
}
refreshBtn.addEventListener('click', fetchPrice);
// Initial fetch
fetchPrice();