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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use csfloat_rs::{
Client, Result,
client::{Category, CreateBuyOrderRequest, CreateListingRequest, ListingType, SortBy, TradeRole},
};
#[tokio::main]
async fn main() -> Result<()> {
// Load environment variables from .env file
dotenvy::dotenv().ok();
// Replace with your actual API key
let api_key = std::env::var("CSFLOAT_API_KEY")
.expect("CSFLOAT_API_KEY environment variable not set");
let client = Client::new(api_key)?;
// Example 1: Get exchange rates
println!("\n=== Example 1: Exchange Rates ===");
let rates = client.get_exchange_rates().await?;
println!("Exchange rates: {:?}", rates);
// Example 2: Get user profile
println!("\n=== Example 2: User Profile ===");
let me = client.get_me().await?;
println!("Username: {:?}", me.user.username);
println!("Balance: {:?} cents", me.user.balance);
println!("Steam ID: {:?}", me.user.steam_id);
// Example 3: Simple listing search
println!("\n=== Example 3: Simple Listing Search ===");
let response = client
.get_all_listings()
.min_price(100)
.max_price(1000)
.limit(5)
.send()
.await?;
println!("Found {} listings:", response.data.len());
for listing in &response.data {
println!(
" - ID: {}, Price: {:.2} cents, Item: {:?}",
listing.id,
listing.price,
listing.item.market_hash_name
);
}
// Example 4: Advanced listing search with filters
println!("\n=== Example 4: Advanced Search (StatTrak, Low Float) ===");
let response = client
.get_all_listings()
.category(Category::StatTrak)
.min_float(0.0)
.max_float(0.10)
.sort_by(SortBy::LowestFloat)
.limit(3)
.send()
.await?;
for listing in &response.data {
if let Some(float) = listing.item.float_value {
println!(
" - {:?} - Float: {:.8}",
listing.item.market_hash_name,
float
);
}
}
// Example 5: Get specific listing
println!("\n=== Example 5: Get Specific Listing ===");
if let Some(first_listing) = response.data.first() {
let detailed = client.get_specific_listing(&first_listing.id).await?;
println!("Detailed listing: {}", detailed.id);
println!(" Seller: {:?}", detailed.seller.username);
println!(" State: {}", detailed.state);
if let Some(watchers) = detailed.watchers {
println!(" Watchers: {}", watchers);
}
}
// Example 6: Get similar listings
println!("\n=== Example 6: Similar Listings ===");
if let Some(first_listing) = response.data.first() {
match client.get_similar(&first_listing.id).await {
Ok(similar) => {
println!("Found {} similar listings", similar.len());
for listing in similar.iter().take(3) {
println!(" - ID: {}, Price: {:.2}", listing.id, listing.price);
}
}
Err(e) => println!("Could not fetch similar listings: {}", e),
}
}
// Example 7: Search by market hash name
println!("\n=== Example 7: Search by Market Hash Name ===");
let response = client
.get_all_listings()
.market_hash_name("AK-47 | Redline (Field-Tested)".to_string())
.limit(5)
.sort_by(SortBy::LowestPrice)
.send()
.await?;
println!("AK-47 Redline listings:");
for listing in &response.data {
println!(" - Price: {:.2} cents", listing.price);
}
// Example 8: Buy orders
println!("\n=== Example 8: Buy Orders ===");
if let Some(listing) = response.data.first() {
match client.get_buy_orders(&listing.id, 5).await {
Ok(orders) => {
println!("Buy orders for listing {}:", listing.id);
for order in orders {
println!(" - Price: {:?}, Qty: {:?}", order.price, order.qty);
}
}
Err(e) => println!("Could not fetch buy orders: {}", e),
}
}
// Example 9: Get user's buy orders
println!("\n=== Example 9: My Buy Orders ===");
match client.get_my_buy_orders(0, 5).await {
Ok(my_orders) => {
println!("Your buy orders: {:?}", my_orders);
}
Err(e) => println!("Could not fetch your buy orders: {}", e),
}
// Example 10: Get stall (user's listings)
println!("\n=== Example 10: User Stall ===");
if let Some(steam_id) = &me.user.steam_id {
match client.get_stall(steam_id, 10).await {
Ok(stall) => {
println!("Your stall has {} listings", stall.data.len());
for listing in stall.listings().iter().take(3) {
println!(" - {:?} - {:.2} cents", listing.item.market_hash_name, listing.price);
}
}
Err(e) => println!("Could not fetch stall: {}", e),
}
}
// Example 11: Get inventory
println!("\n=== Example 11: Inventory ===");
match client.get_inventory().await {
Ok(inventory) => {
println!("Inventory data retrieved (raw): {:?}", inventory);
}
Err(e) => println!("Could not fetch inventory: {}", e),
}
// Example 12: Get watchlist
println!("\n=== Example 12: Watchlist ===");
match client.get_watchlist(10).await {
Ok(watchlist) => {
println!("Watchlist: {:?}", watchlist);
}
Err(e) => println!("Could not fetch watchlist: {}", e),
}
// Example 13: Pagination
println!("\n=== Example 13: Pagination ===");
let mut cursor: Option<String> = None;
let mut total_items = 0;
for page in 0..3 {
let mut builder = client.get_all_listings().limit(10);
if let Some(c) = cursor {
builder = builder.cursor(c);
}
let response = builder.send().await?;
total_items += response.data.len();
println!("Page {}: {} items", page + 1, response.data.len());
cursor = response.cursor;
if cursor.is_none() {
println!("No more pages available");
break;
}
}
println!("Total items fetched: {}", total_items);
// Example 14: Get transactions
println!("\n=== Example 14: Transactions ===");
match client.get_transactions(0, 5).await {
Ok(transactions) => {
println!("Recent transactions: {:?}", transactions);
}
Err(e) => println!("Could not fetch transactions: {}", e),
}
// Example 15: Get trade history
println!("\n=== Example 15: Trade History ===");
match client.get_trade_history(TradeRole::Buyer, 5, 0).await {
Ok(history) => {
println!("Trade history (as buyer): {:?}", history);
}
Err(e) => println!("Could not fetch trade history: {}", e),
}
// Example 16: Get sales history for an item
println!("\n=== Example 16: Sales History ===");
match client.get_sales("AK-47 | Redline (Field-Tested)", None).await {
Ok(sales) => {
println!("Sales history: {:?}", sales);
}
Err(e) => println!("Could not fetch sales history: {}", e),
}
// Example 17: Create buy order (commented out - requires action)
println!("\n=== Example 17: Create Buy Order (Example) ===");
println!("To create a buy order, uncomment the code below:");
/*
let buy_order_request = CreateBuyOrderRequest {
market_hash_name: "AK-47 | Redline (Field-Tested)".to_string(),
max_price: 5000, // 50.00 USD in cents
quantity: 1,
};
match client.create_buy_order(buy_order_request).await {
Ok(result) => println!("Buy order created: {:?}", result),
Err(e) => println!("Failed to create buy order: {}", e),
}
*/
// Example 18: Create listing (commented out - requires action)
println!("\n=== Example 18: Create Listing (Example) ===");
println!("To create a listing, uncomment the code below:");
/*
let listing_request = CreateListingRequest {
asset_id: "YOUR_ASSET_ID".to_string(),
price: 10000.0, // 100.00 USD in cents
listing_type: "buy_now".to_string(),
max_offer_discount: Some(10), // 10% max discount
reserve_price: None,
duration_days: None,
description: "Great item!".to_string(),
private: false,
};
match client.create_listing(listing_request).await {
Ok(result) => println!("Listing created: {:?}", result),
Err(e) => println!("Failed to create listing: {}", e),
}
*/
// Example 19: Make an offer (commented out - requires action)
println!("\n=== Example 19: Make Offer (Example) ===");
println!("To make an offer, uncomment the code below:");
/*
let listing_id = 123456789;
let offer_price = 9500; // 95.00 USD in cents
match client.make_offer(listing_id, offer_price).await {
Ok(result) => println!("Offer made: {:?}", result),
Err(e) => println!("Failed to make offer: {}", e),
}
*/
// Example 20: Buy now (commented out - requires action)
println!("\n=== Example 20: Buy Now (Example) ===");
println!("To buy an item, uncomment the code below:");
/*
let listing_id = 123456789;
let total_price = 10000; // 100.00 USD in cents
match client.buy_now(total_price, listing_id).await {
Ok(result) => println!("Purchase successful: {:?}", result),
Err(e) => println!("Failed to buy: {}", e),
}
*/
// Example 21: Update listing price (commented out - requires action)
println!("\n=== Example 21: Update Listing Price (Example) ===");
println!("To update a listing price, uncomment the code below:");
/*
let listing_id = 123456789;
let new_price = 9500; // 95.00 USD in cents
match client.update_listing_price(listing_id, new_price).await {
Ok(result) => println!("Price updated: {:?}", result),
Err(e) => println!("Failed to update price: {}", e),
}
*/
// Example 22: Delete listing (commented out - requires action)
println!("\n=== Example 22: Delete Listing (Example) ===");
println!("To delete a listing, uncomment the code below:");
/*
let listing_id = 123456789;
match client.delete_listing(listing_id).await {
Ok(result) => println!("Listing deleted: {:?}", result),
Err(e) => println!("Failed to delete listing: {}", e),
}
*/
// Example 23: Delete buy order (commented out - requires action)
println!("\n=== Example 23: Delete Buy Order (Example) ===");
println!("To delete a buy order, uncomment the code below:");
/*
let buy_order_id = 123456789;
match client.delete_buy_order(buy_order_id).await {
Ok(result) => println!("Buy order deleted: {:?}", result),
Err(e) => println!("Failed to delete buy order: {}", e),
}
*/
// Example 24: Complex search with multiple filters
println!("\n=== Example 24: Complex Search ===");
let response = client
.get_all_listings()
.market_hash_name("AWP | Asiimov (Field-Tested)".to_string())
.min_float(0.18)
.max_float(0.25)
.min_price(5000)
.max_price(15000)
.sort_by(SortBy::BestDeal)
.category(Category::Normal)
.limit(10)
.send()
.await?;
println!("Complex search results:");
for listing in &response.data {
if let Some(float) = listing.item.float_value {
println!(
" - Price: {:.2}, Float: {:.8}, Seller: {:?}",
listing.price,
float,
listing.seller.username
);
}
}
// Example 25: Error handling
println!("\n=== Example 25: Error Handling ===");
match client.get_specific_listing("999999999999").await {
Ok(listing) => println!("Found listing: {}", listing.id),
Err(csfloat_rs::Error::NotFound) => println!("Listing not found (expected)"),
Err(csfloat_rs::Error::Unauthorized) => println!("Invalid API key"),
Err(csfloat_rs::Error::TooManyRequests) => println!("Rate limited"),
Err(e) => println!("Other error: {}", e),
}
println!("\n=== All Examples Completed ===");
Ok(())
}