{"id":51061337,"url":"https://github.com/dereckscompany/coinbase","last_synced_at":"2026-06-23T02:04:15.869Z","repository":{"id":361465413,"uuid":"1254562492","full_name":"dereckscompany/coinbase","owner":"dereckscompany","description":"R API wrapper to the Coinbase Advanced Trade API supporting both synchronous and asynchronous (promise based) operations. Provides R6 classes for market data, spot trading, account management, and US futures (CFM), with helpers for tick-to-OHLCV aggregation.","archived":false,"fork":false,"pushed_at":"2026-05-30T18:44:54.000Z","size":75,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-05-30T20:12:51.315Z","etag":null,"topics":["pkgdown","r","rcpp","testthat"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dereckscompany.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-30T18:14:38.000Z","updated_at":"2026-05-30T18:44:58.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/dereckscompany/coinbase","commit_stats":null,"previous_names":["dereckscompany/coinbase"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/dereckscompany/coinbase","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dereckscompany%2Fcoinbase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dereckscompany%2Fcoinbase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dereckscompany%2Fcoinbase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dereckscompany%2Fcoinbase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dereckscompany","download_url":"https://codeload.github.com/dereckscompany/coinbase/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dereckscompany%2Fcoinbase/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34672270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-23T02:00:07.161Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pkgdown","r","rcpp","testthat"],"created_at":"2026-06-23T02:04:15.129Z","updated_at":"2026-06-23T02:04:15.863Z","avatar_url":"https://github.com/dereckscompany.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n```{r setup, include = FALSE}\nknitr::opts_chunk$set(\n    warning = FALSE,\n    message = FALSE,\n    fig.path = \"./man/figures/README-\",\n    fig.align = \"center\",\n    fig.width = 12,\n    fig.height = 10,\n    dpi = 150,\n    results = \"hold\",\n    comment = \"#\u003e\"\n)\n\n# Package symbols used throughout, plus the shared mock HTTP router.\nbox::use(\n    coinbase[\n        CoinbaseMarketData,\n        CoinbaseAccount,\n        CoinbaseTrading,\n        CoinbaseFutures,\n        trades_to_ohlcv,\n        coinbase_backfill_trades\n    ],\n    coro,\n    later,\n    ./tests/testthat/mock_router[mock_router]\n)\n\n# httr2 exposes a native global mock hook: this intercepts every\n# req_perform / req_perform_promise call and answers from canned fixtures, so\n# the document renders against deterministic data with no network, no real\n# credentials, and no funds.\noptions(httr2_mock = mock_router)\n\n# coinbase signs a JWT with the api_private_key BEFORE the request is performed,\n# so a key must still be loadable. We generate a THROWAWAY ephemeral EC P-256\n# key for this build: it signs fine, the mock ignores the Authorization header,\n# it grants nothing, and it is regenerated on every render.\n.sk \u003c- openssl::ec_keygen(\"P-256\")\nKEYS \u003c- list(\n    api_key_name = \"organizations/00000000-0000-0000-0000-000000000000/apiKeys/00000000-0000-0000-0000-000000000000\",\n    api_private_key = openssl::write_pem(.sk)\n)\n```\n\n# coinbase\n\nR API wrapper to the Coinbase Advanced Trade API supporting both synchronous and asynchronous (promise based) operations. Provides R6 classes for market data, spot trading, account management, and US futures (CFM), with helpers for tick-to-OHLCV aggregation.\n\n## Disclaimer\n\nThis software is provided for educational and research purposes. Trading cryptocurrency carries substantial risk. You are solely responsible for any orders placed through this package. Use the order **preview** methods (which execute nothing) before placing live orders.\n\n## Design Philosophy\n\n- **`data.table` everywhere, no list columns.** Every method returns a flat `data.table`; nested API objects are flattened into scalar columns.\n- **Sync and async.** Every method works in both modes. `async = TRUE` returns a [promise][promises::promise]; otherwise results are returned directly. There is a single sync/async branch point.\n- **Exact money values.** Prices, sizes, and amounts are transmitted with full precision (never rounded or in scientific notation).\n- **Two hosts.** Authenticated trading/account endpoints use the Advanced Trade host (`api.coinbase.com`); deep public market data uses the Exchange host (`api.exchange.coinbase.com`).\n\n## Installation\n\n```{r install, eval = FALSE}\n# install.packages(\"remotes\")\nremotes::install_github(\"dereckscompany/coinbase\")\n```\n\n## Setup\n\nCreate API credentials at \u003chttps://www.coinbase.com/settings/api\u003e (download the JSON with a `name` and a `privateKey`). Store them as environment variables in `.Renviron` — the PEM newlines escaped as `\\n` on a single line (see `.Renviron.example`):\n\n```bash\nCOINBASE_API_KEY_NAME=\"organizations/\u003corg-uuid\u003e/apiKeys/\u003ckey-uuid\u003e\"\nCOINBASE_API_PRIVATE_KEY=\"-----BEGIN EC PRIVATE KEY-----\\n...\\n-----END EC PRIVATE KEY-----\\n\"\n```\n\nLoad them with `get_api_keys()` (reads the two environment variables by default):\n\n```{r keys-example, eval = FALSE}\nbox::use(coinbase[ get_api_keys ])\n\nkeys \u003c- get_api_keys()\n```\n\nPublic market data needs no credentials.\n\n## Quick Start — Market Data (no auth)\n\n```{r market}\nmarket \u003c- CoinbaseMarketData$new()\n\n# Best bid/ask\nmarket$get_ticker(\"BTC-USD\")\n```\n\n```{r market-ohlcv}\n# OHLCV candles\nmarket$get_ohlcv(\"BTC-USD\", granularity = \"1min\")\n```\n\n```{r market-trades}\n# Recent tick trades\nmarket$get_trades(\"BTC-USD\", limit = 100)\n```\n\n```{r market-book}\n# Order book (top of book, aggregated)\nmarket$get_orderbook(\"BTC-USD\", level = 2)\n```\n\n```{r market-stats}\n# 24h stats for every product in one call -- rank it yourself for a\n# movers / most-active scanner\nstats \u003c- market$get_stats()\nhead(stats[order(-volume)], 5)\n```\n\nDeep tick history pages the trades endpoint backwards in time; aggregate the\nresult to OHLCV at any timeframe with `trades_to_ohlcv()`:\n\n```{r market-history}\nticks \u003c- market$get_trades(\"BTC-USD\", limit = 100)\nbars \u003c- trades_to_ohlcv(ticks, interval = 60)\nbars[]\n```\n\n## Account (auth)\n\n```{r account}\naccount \u003c- CoinbaseAccount$new()\n```\n\n```{r account-hidden, echo = FALSE}\naccount \u003c- CoinbaseAccount$new(keys = KEYS)\n```\n\n```{r account-balances}\n# Balances across all wallets (paginated)\naccount$get_accounts()\n```\n\n```{r account-fees}\n# Maker/taker fee tier\naccount$get_fees()\n```\n\n```{r account-perms}\naccount$get_key_permissions()\n```\n\n## Trading\n\nAlways validate with a **preview** (which places nothing) before a live order.\n\n```{r trading}\ntrading \u003c- CoinbaseTrading$new()\n```\n\n```{r trading-hidden, echo = FALSE}\ntrading \u003c- CoinbaseTrading$new(keys = KEYS)\n```\n\n```{r trading-preview}\n# Dry run -- executes nothing\ntrading$preview_order(\n  \"BTC-USD\", \"BUY\",\n  list(market_market_ioc = list(quote_size = \"10\"))\n)\n```\n\n```{r trading-add}\n# Place an order (live -- against the mock here)\norder \u003c- trading$add_order(\n  \"BTC-USD\", \"BUY\",\n  list(limit_limit_gtc = list(base_size = \"0.001\", limit_price = \"70000\"))\n)\norder[]\n```\n\n```{r trading-orders}\ntrading$get_orders(product_ids = \"BTC-USD\", limit = 10)\n```\n\n```{r trading-fills}\ntrading$get_fills(product_ids = \"ETH-USD\")\n```\n\n```{r trading-edit}\n# Edit an open order's price or size (preview first, then apply)\ntrading$preview_edit_order(order$order_id, price = \"71000\")\ntrading$edit_order(order$order_id, price = \"71000\")\n```\n\n```{r trading-cancel}\ntrading$cancel_orders(order$order_id)\n```\n\n## US Futures (CFM) — the short leg\n\nUS residents short via CFTC-regulated futures (Coinbase Financial Markets). Futures orders go through the same order endpoint with a futures `product_id`; `CoinbaseFutures` manages the account, positions, and margin.\n\n```{r futures}\nfutures \u003c- CoinbaseFutures$new()\n```\n\n```{r futures-hidden, echo = FALSE}\nfutures \u003c- CoinbaseFutures$new(keys = KEYS)\n```\n\n```{r futures-balance}\nfutures$get_balance_summary()\n```\n\n```{r futures-positions}\nfutures$get_positions()\n```\n\n```{r futures-sweeps}\nfutures$get_sweeps()\n```\n\n```{r futures-short, eval = FALSE}\n# Open a short on a futures product (live -- placed through CoinbaseTrading)\n# CoinbaseTrading$new()$add_order(\n#   \"BIT-31OCT26-CDE\", \"SELL\",\n#   list(market_market_ioc = list(base_size = \"1\"))\n# )\n```\n\n## Bulk Backfill\n\n```{r backfill, eval = FALSE}\n# Walks trades back to `from`, writes CSV incrementally, resumes if re-run.\ncoinbase_backfill_trades(\n  symbols = c(\"BTC-USD\", \"ETH-USD\"),\n  from = lubridate::as_datetime(\"2026-05-01\", tz = \"UTC\"),\n  file = \"trades.csv\"\n)\n```\n\n## Asynchronous Use\n\nThe package is written around promises for non-blocking, event-loop use (à la\nJavaScript). Pass `async = TRUE` to any class and its methods return a\n[promise][promises::promise] instead of a `data.table`. Resolve it with\n`$then()` chaining or, as recommended, `coro::async()` / `await()` for\nsequential-looking code, and drive the event loop with\n[later](https://r-lib.github.io/later/).\n\n```{r async}\nmarket_async \u003c- CoinbaseMarketData$new(async = TRUE)\n\nmain \u003c- coro$async(function() {\n  ticker \u003c- await(market_async$get_ticker(\"BTC-USD\"))\n  ohlcv \u003c- await(market_async$get_ohlcv(\"BTC-USD\", granularity = \"1min\"))\n\n  print(ticker)\n  print(ohlcv)\n})\n\nmain()\n\n# Drain the event loop until every promise has resolved.\nwhile (!later$loop_empty()) {\n  later$run_now()\n}\n```\n\n## Available Classes\n\n| Class | Purpose | Auth |\n|-------|---------|------|\n| `CoinbaseMarketData` | products, OHLCV, trades, order book, ticker, deep history | No |\n| `CoinbaseAccount` | balances, fees, portfolios, permissions | Yes |\n| `CoinbaseTrading` | place / preview / edit / cancel / query orders and fills | Yes |\n| `CoinbaseFutures` | US futures (CFM) balances, positions, margin, sweeps | Yes |\n\n## License\n\nMIT © Dereck Mezquita\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdereckscompany%2Fcoinbase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdereckscompany%2Fcoinbase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdereckscompany%2Fcoinbase/lists"}