Skip to content

lucianboboc/btc-price-api

Repository files navigation

Bitcoin Price API

A simple REST API to get the Last Traded Price for Bitcoin.

Overview

The API provides a simple and efficient way to get the last traded price for Bitcoin from an external source (Kraken), and it has up to last minute accuracy.

Features

  • Fast and efficient REST API
  • PostgreSQL database for the price data persistent storage
  • Redis for caching data
  • Database migrations using Goose

Prerequisites

  • Go 1.24 or higher
  • Docker and Docker Compose

Setup and Installation

  1. Clone the repository:
git clone https://github.com/lucianboboc/btc-price-api
cd btc-price-api
  1. Create an .env file
cp .env.example .env
  1. Start the app using Docker Compose
docker compose up -d --build

The API will be available at http://localhost:8080/api/v1/ltp

API Documentation

  • GET /api/v1/ltp - Get the last traded price for Bitcoin
{
  "ltp":
  [
    {
      "pair": "BTC/CHF",
      "amount": "77723.60"
    },
    {
      "pair": "BTC/EUR",
      "amount": "82569.10"
    },
    {
      "pair": "BTC/USD",
      "amount": "93744.10"
    }
  ]
}

Database Schema

Pairs Table

CREATE TABLE pairs(
    id serial PRIMARY KEY,
    pair_symbol text NOT NULL UNIQUE,  -- ex: BTC/CHF 
    base_currency text NOT NULL,       -- ex: BTC
    currency text NOT NULL
);

Prices Table

CREATE TABLE prices(
   id serial PRIMARY KEY,
   pair_id int NOT NULL UNIQUE,
   price_in_cents bigint NOT NULL,
   timestamp timestamp(0) with time zone NOT NULL DEFAULT NOW(),
   FOREIGN KEY (pair_id) REFERENCES pairs(id) ON DELETE CASCADE
);

Error Handling

The api returns the following JSON error format using apierrors.APIError type:

{
  "error": {
    "domain": "btc",
    "status_code": 500,
    "message": "Internal server error",
    "key": "InternalServerError"
  }
}

Architecture

The project is built using the following architecture:

  • cmd/api - Application entry point, it handles db creation, it will run migrations and start the HTTP server.
  • config - Configuration management, it is responsible for loading of environment variables, and it creates the config files required to configure the app.
  • internal/domain - Core business logic and domain models including abstractions like Cache and Repository that define contracts for the infrastructure layer.
  • internal/infrastructure - Infrastructure layer contains implementation for external third-party services like Kraken and access to the database or interacting with cache.
  • internal/application - Application layer is using abstractions like Fetcher for the Kraken external service to fetch the Bitcoin price and handles the domain services interaction with the infrastructure layer using Cache or Repository.
  • internal/transport - HTTP transport layer manages handlers, routes, it exposes endpoints and handle serialization/deserialization for data transport.
  • pkg/apierrors - Custom error types exposed to the clientside
  • migrations - Database migration files used to modify the database schema using the goose migration tool.

License

This project is licensed under the MIT License.

About

Last Traded Price of Bitcoin API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published