coinglecko

Package Version Hex Docs

A type-safe CoinGecko API client for Gleam.

Works on both Erlang and JavaScript targets.

Installation

gleam add coinglecko@1

You also need an HTTP client for your target:

# Erlang
gleam add gleam_httpc@5

# JavaScript
gleam add gleam_fetch@1

Quick Start (Erlang)

import coinglecko/client
import coinglecko/ping
import coinglecko/simple
import gleam/httpc
import gleam/io
import gleam/option.{None}
import gleam/result
import gleam/string

pub fn main() {
  let send = fn(req) { httpc.send(req) |> result.map_error(string.inspect) }
  let client = client.new_demo(api_key: "CG-your-key-here")

  // Check server status
  let assert Ok(pong) = ping.check(client, send:)
  io.println(pong.gecko_says)

  // Get Bitcoin price in USD
  let assert Ok(prices) = simple.price(
    client,
    ids: ["bitcoin"],
    vs_currencies: ["usd"],
    include_market_cap: None,
    include_24hr_vol: None,
    include_24hr_change: None,
    include_last_updated_at: None,
    precision: None,
    send:,
  )
}

JavaScript Usage

On JavaScript, use the request-building and decoding functions directly with gleam_fetch:

import coinglecko/client
import coinglecko/ping
import gleam/fetch
import gleam/javascript/promise

pub fn main() {
  let client = client.new_demo(api_key: "CG-your-key-here")

  // Build request (no HTTP call)
  let assert Ok(req) = ping.check_request(client)

  // Send with fetch (async)
  use resp <- promise.try_await(fetch.send(req))
  use body <- promise.try_await(fetch.read_text_body(resp))

  // Decode response
  let assert Ok(pong) = ping.decode_response(body.body)
  promise.resolve(Ok(pong))
}

Available Modules

ModuleDescription
coinglecko/clientClient construction (new_demo, new_pro)
coinglecko/errorError types (CoinGeckoError)
coinglecko/pingServer status check
coinglecko/simpleCurrent prices, supported currencies, token prices
coinglecko/coinsCoin list, markets, details, charts, OHLC, tickers, history, contract
coinglecko/searchSearch coins/exchanges, trending coins
coinglecko/globalGlobal market data, DeFi data
coinglecko/exchangesExchange list, details, tickers
coinglecko/categoriesCoin categories with market data
coinglecko/asset_platformsBlockchain platform list
coinglecko/nftsNFT collections, details, market charts
coinglecko/derivativesDerivatives tickers, exchanges
coinglecko/exchange_ratesBTC exchange rates
coinglecko/companiesPublic company treasury holdings
coinglecko/paginationPage params, collect_all, collect_each
coinglecko/rate_limitRate limit header parsing
coinglecko/retryConfigurable retry with exponential backoff logic

Three-Layer API

Each endpoint provides three layers:

  1. *_request(client, ...) – Builds an HTTP Request. Use this on JS or when you need full control.
  2. decode_*(json_string) – Decodes a JSON response string. For testing or manual flows.
  3. foo(client, ..., send:) – Convenience: builds, sends, and decodes in one call.

API Key

Get a free API key at coingecko.com/en/api.

Error Handling

All API functions return Result(value, CoinGeckoError):

Development

gleam build                      # Compile (Erlang)
gleam build --target javascript  # Compile (JS)
gleam test                       # Run tests
gleam format                     # Format code

Further documentation can be found at https://hexdocs.pm/coinglecko.

Search Document