Skip to main content

Overview

This endpoint demonstrates advanced data transformation capabilities by aggregating cryptocurrency market data and global statistics, while using flatmap filters to clean and reshape the response. HTTP Method: GET
Endpoint: /cryptos/{currency}

Parameters

currency
string
required
The fiat currency to display prices in (e.g., “usd”, “eur”, “gbp”)

What It Demonstrates

  • Aggregation with Collections: Combines cryptocurrency data with global market statistics
  • Advanced Transformation: Uses flatmap filters to move and delete fields
  • Safe JSON Encoding: Handles potentially unsafe JSON responses
  • Field Filtering: Selects specific nested fields from global data
  • Data Grouping: Organizes global statistics under market key

Request Example

curl http://localhost:8080/cryptos/usd
curl http://localhost:8080/cryptos/eur

Expected Response

{
  "coins": [
    {
      "id": "bitcoin",
      "symbol": "btc",
      "name": "Bitcoin",
      "image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png",
      "current_price": 43250.50,
      "market_cap": 846000000000,
      "ath": 69045
    },
    {
      "id": "ethereum",
      "symbol": "eth",
      "name": "Ethereum",
      "image": "https://assets.coingecko.com/coins/images/279/large/ethereum.png",
      "current_price": 2890.75,
      "market_cap": 347000000000,
      "ath": 4878
    }
  ],
  "market": {
    "total_market_cap": {
      "btc": 52345678.90,
      "eth": 789012345.67
    },
    "total_volume": {
      "btc": 1234567.89,
      "eth": 9876543.21
    },
    "market_cap_percentage": {
      "btc": 52.5,
      "eth": 18.3
    },
    "updated_at": 1709481600
  }
}

Backend Services Called

1. CoinGecko Market Data

  • Host: https://api.coingecko.com
  • URL Pattern: /api/v3/coins/markets?vs_currency={currency}&ids=bitcoin%2Cethereum&order=market_cap_desc&per_page=100&page=1&sparkline=false
  • Purpose: Fetches current market data for Bitcoin and Ethereum
  • Encoding: safejson

2. CoinGecko Global Data

  • Host: https://api.coingecko.com
  • URL Pattern: /api/v3/global
  • Purpose: Fetches global cryptocurrency market statistics
  • Target: data (extracts data from the nested response)
  • Group: market

KrakenD Configuration

{
  "@comment": "Feature: Aggregation & Transformation with collections",
  "endpoint": "/cryptos/{currency}",
  "backend": [
    {
      "host": ["https://api.coingecko.com"],
      "url_pattern": "/api/v3/coins/markets?vs_currency={currency}&ids=bitcoin%2Cethereum&order=market_cap_desc&per_page=100&page=1&sparkline=false",
      "encoding": "safejson",
      "extra_config": {
        "proxy": {
          "flatmap_filter": [
            {
              "type": "move",
              "args": ["collection", "coins"]
            },
            {
              "type": "del",
              "args": [
                "coins.*.ath_change_percentage",
                "coins.*.ath_date",
                "coins.*.atl",
                "coins.*.atl_change_percentage",
                "coins.*.atl_date",
                "coins.*.circulating_supply",
                "coins.*.fully_diluted_valuation",
                "coins.*.high_24h",
                "coins.*.low_24h",
                "coins.*.market_cap_change_24h",
                "coins.*.market_cap_change_percentage_24h",
                "coins.*.market_cap_rank",
                "coins.*.max_supply",
                "coins.*.price_change_24h",
                "coins.*.price_change_percentage_24h",
                "coins.*.roi",
                "coins.*.total_supply",
                "coins.*.total_volume"
              ]
            }
          ]
        }
      }
    },
    {
      "host": ["https://api.coingecko.com"],
      "url_pattern": "/api/v3/global",
      "allow": [
        "total_market_cap.btc",
        "total_market_cap.eth",
        "total_volume.btc",
        "total_volume.eth",
        "market_cap_percentage.btc",
        "market_cap_percentage.eth",
        "updated_at"
      ],
      "target": "data",
      "group": "market"
    }
  ]
}

Key Configuration Options

encoding: "safejson"

Handles potentially unsafe JSON responses from the CoinGecko API, preventing issues with special characters or malformed data.

flatmap_filter

Advanced transformation operations applied to the backend response: Move Operation:
{
  "type": "move",
  "args": ["collection", "coins"]
}
Moves the collection array to a coins field in the response. Delete Operation:
{
  "type": "del",
  "args": ["coins.*.ath_change_percentage", ...]
}
Removes unnecessary fields from each coin object in the collection, reducing payload size.

target

Extracts a specific field from the backend response. The global endpoint returns {data: {...}}, so target: "data" extracts the inner object.

allow with Nested Fields

Filters nested fields using dot notation (e.g., total_market_cap.btc), allowing precise control over complex response structures.

Use Cases

  • Cryptocurrency price tracking applications
  • Portfolio management systems
  • Market analysis dashboards
  • Trading platform integrations
  • Financial data aggregation services

Build docs developers (and LLMs) love