A simple REST API to get the Last Traded Price for Bitcoin.
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.
- Fast and efficient REST API
- PostgreSQL database for the price data persistent storage
- Redis for caching data
- Database migrations using Goose
- Go 1.24 or higher
- Docker and Docker Compose
- Clone the repository:
git clone https://github.com/lucianboboc/btc-price-api
cd btc-price-api
- Create an .env file
cp .env.example .env
- Start the app using Docker Compose
docker compose up -d --build
The API will be available at http://localhost:8080/api/v1/ltp
- 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"
}
]
}
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
);
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
);
The api returns the following JSON error format using apierrors.APIError
type:
{
"error": {
"domain": "btc",
"status_code": 500,
"message": "Internal server error",
"key": "InternalServerError"
}
}
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 likeCache
andRepository
that define contracts for the infrastructure layer.internal/infrastructure
- Infrastructure layer contains implementation for external third-party services likeKraken
and access to the database or interacting with cache.internal/application
- Application layer is using abstractions likeFetcher
for the Kraken external service to fetch the Bitcoin price and handles the domain services interaction with the infrastructure layer usingCache
orRepository
.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 clientsidemigrations
- Database migration files used to modify the database schema using thegoose
migration tool.
This project is licensed under the MIT License.