dougdellolio / coinbasepro-csharp

The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
MIT License
193 stars 90 forks source link
api bitcoin coinbase coinbasepro coinbasepro-api csharp-client ethereum gdax gdax-api library wrapper wrapper-api

coinbasepro-csharp

https://docs.pro.coinbase.com/ [![Build status](https://ci.appveyor.com/api/projects/status/mdn0y9cu7dqsambv/branch/master?svg=true)](https://ci.appveyor.com/project/dougdellolio/coinbasepro-csharp/branch/master) [![NuGet](https://img.shields.io/nuget/v/GDAX.Api.ClientLibrary.svg)](https://www.nuget.org/packages/GDAX.Api.ClientLibrary/) [![NuGet](https://img.shields.io/nuget/dt/GDAX.Api.ClientLibrary.svg)](https://www.nuget.org/packages/GDAX.Api.ClientLibrary/)

How to Install

PM> Install-Package GDAX.Api.ClientLibrary

How to Use

Generate your key at https://pro.coinbase.com/profile/api

//create an authenticator with your apiKey, apiSecret and passphrase
var authenticator = new Authenticator("<apiKey>", "<apiSecret>", "<passphrase>");

//create the CoinbasePro client
var coinbaseProClient = new CoinbasePro.CoinbaseProClient(authenticator);

//use one of the services 
var allAccounts = await coinbaseProClient.AccountsService.GetAllAccountsAsync();

What services are provided?

Accounts
CoinbaseAccounts
Orders
Payments
Withdrawals
Deposits
Products
Currencies
Fills
Limits
Fundings
Reports
User Account
Fees
Stablecoin Conversions
Profiles

Websocket Feed

How to use with authentication

//create an authenticator with your apiKey, apiSecret and passphrase
var authenticator = new Authenticator("<apiKey>", "<apiSecret>", "<passphrase>");

//create the CoinbasePro client
var coinbaseProClient = new CoinbasePro.CoinbaseProClient(authenticator);

//use the websocket feed
var productTypes = new List<string>() { "BTC-EUR", "BTC-USD" };
var channels = new List<ChannelType>() { ChannelType.Full, ChannelType.User } // When not providing any channels, the socket will subscribe to all channels

var webSocket = coinbaseProClient.WebSocket;
webSocket.Start(productTypes, channels);

// EventHandler for the heartbeat response type
webSocket.OnHeartbeatReceived += WebSocket_OnHeartbeatReceived;

private static void WebSocket_OnHeartbeatReceived(object sender, WebfeedEventArgs<Heartbeat> e)
{
  throw new NotImplementedException();
}

How to use without authentication

//create the CoinbasePro client without an authenticator
var coinbaseProClient = new CoinbasePro.CoinbaseProClient();

//use the websocket feed
var productTypes = new List<string>() { "BTC-EUR", "BTC-USD" };
var channels = new List<ChannelType>() { ChannelType.Full, ChannelType.User }; // When not providing any channels, the socket will subscribe to all channels

var webSocket = coinbaseProClient.WebSocket;
webSocket.Start(productTypes, channels);

// EventHandler for the heartbeat response type
webSocket.OnHeartbeatReceived += WebSocket_OnHeartbeatReceived;

private static void WebSocket_OnHeartbeatReceived(object sender, WebfeedEventArgs<Heartbeat> e)
{
  throw new NotImplementedException();
}

Available functions

These are the starting and stopping methods:

The following methods are EventHandlers:

Sandbox Support

Generate your key at https://public.sandbox.pro.coinbase.com/profile/api

//create an authenticator with your apiKey, signature and passphrase
var authenticator = new Authenticator("<apiKey>", "<signature>", "<passphrase>");

//create the CoinbasePro client and set the sandbox flag to true
var coinbaseProClient = new CoinbasePro.CoinbaseProClient(authenticator, true);

//use one of the services 
var response = await coinbaseProClient.OrdersService.PlaceMarketOrderAsync(OrderSide.Buy, "BTC-USD", 1);

Examples

Place a market order
//by size
var response = await coinbaseProClient.OrdersService.PlaceMarketOrderAsync(OrderSide.Buy, "BTC-USD", 1);

//by funds
var response = await coinbaseProClient.OrdersService.PlaceMarketOrderAsync(OrderSide.Buy, "BTC-USD", 50, MarketOrderAmountType.Funds);
Place a limit order
var response = await coinbaseProClient.OrdersService.PlaceLimitOrderAsync(OrderSide.Sell, "ETH-USD", 1, 400.0M);
Cancel all open or un-settled orders
var response = await coinbaseProClient.OrdersService.CancelAllOrdersAsync();
Getting account history (paged response)
//the limit is the amount of items per page - in this case it would be 2 items (default is 100)
//you can also specify the number of pages to request - in this case it would be the first 5 pages (default is 0 which will request all pages)
//some routes may require the number of pages to be specified as there are rate limits
var accountHistoryResponse = await coinbaseProClient.AccountsService.GetAccountHistoryAsync("ef56a389", 2, 5);

//retrieve by page number - this would return the first page of the response (latest first)
var firstPage = accountHistoryResponse.ToList()[0];

//get the first item on the page
var firstAccountHistoryOnFirstPage = firstPage.ToList()[0];

//get the second item on the page
var secondAccountHistoryOnFirstPage = firstPage.ToList()[1];
Generate and email a report
var reportDateFrom = new DateTime(2017, 1, 1);
var reportDateTo = new DateTime(2018, 1, 1);
var accountId = "29318029382";

//generate and email accounts report csv
var accountResponse = coinbaseProClient.ReportsService.CreateNewAccountReportAsync(reportDateFrom, reportDateTo, accountId, "BTC-USD", "me@email.com", FileFormat.Csv);

//generate and email fills report pdf
var fillsResponse = coinbaseProClient.ReportsService.CreateNewFillsReportAsync(reportDateFrom, reportDateTo, "BTC-USD", accountId, "me@email.com", FileFormat.Pdf);
Overriding the HttpClient behavior

You can gain greater control of the http requests by implementing CoinbasePro.HttpClient.IHttpClient and passing that into the CoinbasePro constructor.

var myWay = new MyNamespace.MyHttpClient();

//create an authenticator with your apiKey, signature and passphrase
var authenticator = new Authenticator("<apiKey>", "<signature>", "<passphrase>");

//create the CoinbasePro client and set the httpClient to my way of behaving
var coinbaseProClient = new CoinbasePro.CoinbaseProClient(authenticator, myWay);

Logging

Logging is provided by Serilog - https://github.com/serilog/serilog

//configure the application logging to output to console and a file called log.txt
Serilog.Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File("log.txt",
                    rollingInterval: RollingInterval.Day,
                    rollOnFileSizeLimit: true)
                .CreateLogger();

//create an authenticator with your apiKey, signature and passphrase
var authenticator = new Authenticator("<apiKey>", "<signature>", "<passphrase>");

//create the CoinbasePro client
var coinbaseProClient = new CoinbasePro.CoinbaseProClient(authenticator);

//use one of the services 
var response = await coinbaseProClient.OrdersService.PlaceMarketOrderAsync(OrderSide.Buy, "BTC-USD", 1);

Contributors

Thanks for contributing!

Bugs or questions?

Please open an issue for any bugs or questions