Build a compact CoinGecko request using asset IDs, a target currency and an update timestamp.
A ticker may identify more than one asset. This example uses the provider’s stable asset IDs and asks for last_updated_at alongside the requested currency. The code requires the Demo API plan and sends its key in a header. It prints the returned data and stops if a requested asset or price is missing.
Before you start
Python 3.10 or later and access to the CoinGecko Demo API.
A Demo API key supplied by your own environment as COINGECKO_DEMO_API_KEY.
CoinGecko asset IDs checked against the provider’s coins list. Contract identity still comes from the relevant network and official project.
Build and check
Choose one to ten asset IDs and select USD or EUR in the builder.
Inspect the query. The example removes duplicate IDs and includes an update timestamp.
Configure the Demo API key in your local environment and run the file once.
Check last_updated_at and missing fields before using a returned value. Apply the provider’s attribution, rate and caching requirements to a deployed application.
Limits to understand
The page does not display live market prices or make a market-data request.
Provider IDs are not contract addresses. Match the chain and contract separately when needed.
A timestamp is evidence to inspect, not an automatic guarantee that a price is current. Do not treat the sample as trading guidance.
ADJUST THE EXAMPLE
Change a setting. Inspect the result.
This browser preview builds code locally. It does not contact an API or run the code.
Default example ready.
Python example
"""Query stable asset IDs with a CoinGecko Demo key supplied by your own environment."""
import json
import os
import re
from urllib.parse import urlencode
from urllib.request import Request, urlopen
IDS = ["bitcoin", "ethereum", "solana"]
CURRENCY = "usd"
def get_prices(ids=IDS, currency=CURRENCY):
ids = list(dict.fromkeys(ids))
if not 1 <= len(ids) <= 10 or any(not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,79}", value) for value in ids):
raise ValueError("Use one to ten valid CoinGecko asset IDs.")
if currency not in {"usd", "eur"}:
raise ValueError("This example supports USD or EUR.")
key = os.environ.get("COINGECKO_DEMO_API_KEY")
if not key:
raise RuntimeError("A CoinGecko Demo API key is required in your environment.")
query = urlencode({"ids": ",".join(ids), "vs_currencies": currency, "include_last_updated_at": "true"})
request = Request("https://api.coingecko.com/api/v3/simple/price?" + query, headers={"x-cg-demo-api-key": key})
with urlopen(request, timeout=15) as reply:
data = json.load(reply)
if not isinstance(data, dict):
raise RuntimeError("The provider response must be a JSON object.")
missing = [asset for asset in ids if not isinstance(data.get(asset), dict) or type(data[asset].get(currency)) not in {int, float} or data[asset][currency] < 0]
if missing:
raise RuntimeError(f"Missing requested assets or prices: {missing}")
return data
if __name__ == "__main__":
print(json.dumps(get_prices(), indent=2))
What to verify after running
Duplicate IDs produce one requested asset each.
The key is a request header and does not enter the generated URL.
Missing assets and HTTP failures are surfaced; no price is filled in from a placeholder.
Request or format preview
Illustrative preview. No provider response, price or balance is fetched.