17 releases

0.4.4 Mar 11, 2024
0.4.1 Jan 18, 2024
0.4.0 Sep 1, 2023
0.3.0 Dec 20, 2022
0.2.1 Jul 31, 2022

#677 in Magic Beans

Download history 6/week @ 2024-01-15 41/week @ 2024-02-19 17/week @ 2024-02-26 232/week @ 2024-03-04 244/week @ 2024-03-11 7/week @ 2024-03-18 12/week @ 2024-03-25 24/week @ 2024-04-01

303 downloads per month

MIT/Apache

110KB
2K SLoC

cmc

Crates.io docs.rs MIT licensed Apache-2.0 licensed

Unofficial Rust Library for the CoinMarketCap API

Usage

Get your API key here


CoinMarketCap ID Map

NOTE: CoinMarketCap recommend utilizing CMC ID instead of cryptocurrency symbols to securely identify cryptocurrencies with other endpoints and in your own application logic.

use cmc::{Cmc, Sort};

let cmc = Cmc::new("<API KEY>");

match cmc.id_map(1, 50, Sort::CmcRank) {
    Ok(map) => println!("{}", map),
    Err(err) => println!("{}", err),
}

Price cryptocurrency

use cmc::Cmc;

let cmc = Cmc::new("<API KEY>");

match cmc.price("BTC") {
    Ok(price) => println!("Price: {}", price),
    Err(err) => println!("Error: {}", err),
}

Price with custom settings

use cmc::{CmcBuilder, Pass};

let cmc = CmcBuilder::new("<API KEY>")
    .pass(Pass::Id)
    .convert("EUR")
    .build();

match cmc.price("1027") { // 1027 is Ethereum id.
    Ok(price) => println!("Price: {}", price), // In Euro instead default USD
    Err(err) => println!("Error: {}", err),
}

Price conversion

use cmc::Cmc;

let cmc = Cmc::new("<API KEY>");

// 2.5 BTC in EUR (using symbols)
match cmc.price_conversion(2.5, "BTC", None, "EUR") {
    Ok(price) => println!("Total price: {}", price),
    Err(err) => println!("Error: {}", err),
}

// 1.6 ETH in XMR (using id's)
match cmc.price_conversion_id(1.6, "1027", None, "328") {
    Ok(price) => println!("Total price: {}", price),
    Err(err) => println!("Error: {}", err),
}

Exchange ID Map

use cmc::{Cmc, ListingStatusExchange, SortExchange};

let cmc = Cmc::new("<API KEY>");

match cmc.exchange_id_map(ListingStatusExchange::Active, 1, 10, SortExchange::Id, None) {
    Ok(map) => println!("{}", map),
    Err(err) => println!("{}", err),
}

Crate Features

This crate supports default features:

  • cryptocurrency
  • exchange
  • fiat
  • global_metrics
  • key
  • tools

Disable all functions except the necessary ones:

[dependencies]
cmc = { version = "0.4.0", default-features = false, features = ["cryptocurrency"] }

Async

Asynchronous versions of functions are available through enabling the async feature:

[dependencies]
cmc = { version = "0.4.0", features = ["async"] }

And then the code:

#[tokio::main]
async fn main() {
    use cmc::async_api::Cmc;
    
    let cmc = Cmc::new("<API KEY>");
    
    match cmc.price("BTC").await {
        Ok(price) => println!("Price: {}", price),
        Err(err) => println!("Error: {}", err),
    }
}

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4–18MB
~257K SLoC