1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Products API endpoints.
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{
Candle, GetBestBidAskParams, GetBestBidAskResponse, GetCandlesParams, GetCandlesResponse,
GetMarketTradesParams, GetMarketTradesResponse, GetProductBookParams, GetProductBookResponse,
ListProductsParams, ListProductsResponse, Product, ProductBook,
};
/// API for accessing product and market data.
///
/// Products represent trading pairs (e.g., BTC-USD).
/// This API provides access to product information, order books,
/// candles, and recent trades.
pub struct ProductsApi<'a> {
client: &'a RestClient,
}
impl<'a> ProductsApi<'a> {
/// Create a new Products API instance.
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
/// List all products.
///
/// Returns a list of available trading pairs.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::ListProductsParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let products = client.products().list(ListProductsParams::new().limit(10)).await?;
/// for product in products.products {
/// println!("{}: {} @ {}", product.product_id, product.base_name, product.price);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list(&self, params: ListProductsParams) -> Result<ListProductsResponse> {
self.client.get_with_query("/products", ¶ms).await
}
/// List all products with default parameters.
pub async fn list_all(&self) -> Result<ListProductsResponse> {
self.list(ListProductsParams::default()).await
}
/// Get a single product by ID.
///
/// # Arguments
///
/// * `product_id` - The product identifier (e.g., "BTC-USD").
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let product = client.products().get("BTC-USD").await?;
/// println!("BTC price: ${}", product.price);
/// # Ok(())
/// # }
/// ```
pub async fn get(&self, product_id: &str) -> Result<Product> {
let endpoint = format!("/products/{}", product_id);
self.client.get(&endpoint).await
}
/// Get the order book for a product.
///
/// Returns the current bids and asks for the specified product.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::GetProductBookParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let book = client.products()
/// .get_book(GetProductBookParams::new("BTC-USD").limit(10))
/// .await?;
///
/// println!("Best bid: {}", book.bids.first().map(|b| &b.price).unwrap_or(&"N/A".to_string()));
/// # Ok(())
/// # }
/// ```
pub async fn get_book(&self, params: GetProductBookParams) -> Result<ProductBook> {
let response: GetProductBookResponse =
self.client.get_with_query("/product_book", ¶ms).await?;
Ok(response.pricebook)
}
/// Get the best bid/ask for one or more products.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::GetBestBidAskParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let response = client.products()
/// .get_best_bid_ask(GetBestBidAskParams::new().product_ids(&["BTC-USD", "ETH-USD"]))
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_best_bid_ask(
&self,
params: GetBestBidAskParams,
) -> Result<GetBestBidAskResponse> {
self.client.get_with_query("/best_bid_ask", ¶ms).await
}
/// Get candlestick (OHLCV) data for a product.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::{GetCandlesParams, Granularity}};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let candles = client.products()
/// .get_candles(GetCandlesParams::new(
/// "BTC-USD",
/// "1704067200", // Start timestamp
/// "1704153600", // End timestamp
/// Granularity::OneHour
/// ))
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_candles(&self, params: GetCandlesParams) -> Result<Vec<Candle>> {
let endpoint = format!("/products/{}/candles", params.product_id);
let response: GetCandlesResponse =
self.client.get_with_query(&endpoint, ¶ms).await?;
Ok(response.candles)
}
/// Get recent trades for a product.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::GetMarketTradesParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let response = client.products()
/// .get_market_trades(GetMarketTradesParams::new("BTC-USD", 10))
/// .await?;
///
/// for trade in response.trades {
/// println!("{} {} @ {}", trade.side, trade.size, trade.price);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_market_trades(
&self,
params: GetMarketTradesParams,
) -> Result<GetMarketTradesResponse> {
let endpoint = format!("/products/{}/ticker", params.product_id);
self.client.get_with_query(&endpoint, ¶ms).await
}
}