# BrickTrack: SDKs

Official client libraries for the BrickTrack REST API, open source (MIT) on GitHub: https://github.com/bricktrackdev. Every client reads the API key from `BRICKTRACK_API_KEY` and sends it as a Bearer token. Human-readable page: https://www.bricktrack.dev/docs/sdks

## Install

| SDK | Install | Repository |
| --- | --- | --- |
| Node.js / TypeScript | Not on npm yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-node |
| Python | `pip install git+https://github.com/bricktrackdev/bricktrack-python` (Installs it from GitHub.) | https://github.com/bricktrackdev/bricktrack-python |
| Go | `go get github.com/bricktrackdev/bricktrack-go` | https://github.com/bricktrackdev/bricktrack-go |
| Java | Not on Maven Central: build it from its repository with Maven. | https://github.com/bricktrackdev/bricktrack-java |
| .NET | Not on NuGet yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-dotnet |
| Ruby | Not on RubyGems yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-ruby |
| PHP | Not on Packagist yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-php |
| CLI | Not on npm yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-cli |
| MCP server | Not on npm yet: build it from its repository. | https://github.com/bricktrackdev/bricktrack-mcp |

## Quick start (Node.js)

```js
import { BrickTrackClient } from "@bricktrack/sdk";

// Reads your API key from BRICKTRACK_API_KEY.
const client = new BrickTrackClient();

// Track a product and get its first price.
const { data: product } = await client.trackProduct({
  product: "https://www.lego.com/en-us/product/millennium-falcon-75192",
  wait: true,
});
console.log(product.title, product.price, product.currency);

// Its price history, oldest first.
const { data: tracked } = await client.getProduct({ id: product.product_id, days: 90 });
for (const point of tracked.history) console.log(point.checked_at, point.price);

// Any set number's history, tracked or not, without using credits.
const { data: history } = await client.getSetHistory({ set_number: "75192" });
console.log(history.lowest_price, history.highest_price);
```

## Quick start (Python)

```python
from bricktrack import BrickTrackClient

# Reads your API key from BRICKTRACK_API_KEY.
with BrickTrackClient() as client:
    # Track a product and get its first price.
    product = client.track_product(product="https://www.lego.com/en-us/product/millennium-falcon-75192", wait=True).data
    print(product["title"], product["price"], product["currency"])

    # Its price history, oldest first.
    tracked = client.get_product(id=product["product_id"], days=90).data
    for point in tracked["history"]:
        print(point["checked_at"], point["price"])

    # Any set number's history, tracked or not, without using credits.
    history = client.get_set_history(set_number="75192").data
    print(history["lowest_price"], history["highest_price"])
```

## Methods

One method per endpoint, with the API's parameter names. Java and PHP use the Node.js names, Go the same in PascalCase, .NET in PascalCase with Async, and Ruby the Python names.

| Node.js | Python | Endpoint | Does |
| --- | --- | --- | --- |
| `getAccount` | `get_account` | `GET /account` | Plan and remaining credits |
| `searchSets` | `search_sets` | `GET /search` | Search LEGO |
| `getSetHistory` | `get_set_history` | `GET /history` | Price history of any set number |
| `listProducts` | `list_products` | `GET /products` | List tracked products |
| `trackProduct` | `track_product` | `POST /products` | Track a product |
| `getProduct` | `get_product` | `GET /products/{id}` | Get a product and its price history |
| `updateProduct` | `update_product` | `PATCH /products/{id}` | Change alerts and checks |
| `checkProduct` | `check_product` | `POST /products/{id}/check` | Check the price now |
| `deleteProduct` | `delete_product` | `DELETE /products/{id}` | Stop tracking a product |
| `clearTargetPrice` | `clear_target_price` | `PATCH /products/{id}` | Remove a product's target price |

## Errors and retries

A failed request throws an error with the HTTP status and the API's error code, such as insufficient_credits or product_limit: BrickTrackApiError (status, errorCode) in Node.js, BrickTrackAPIError (status, error_code) in Python. Network errors and 408, 500, 502, 503 and 504 answers are retried up to 3 times; 429 and other errors aren't, since rate limits last until the hour is over.
