<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.fintechlab.unibocconi.eu/index.php?action=history&amp;feed=atom&amp;title=Cryptocurrency_data_collection_in_python</id>
	<title>Cryptocurrency data collection in python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.fintechlab.unibocconi.eu/index.php?action=history&amp;feed=atom&amp;title=Cryptocurrency_data_collection_in_python"/>
	<link rel="alternate" type="text/html" href="https://wiki.fintechlab.unibocconi.eu/index.php?title=Cryptocurrency_data_collection_in_python&amp;action=history"/>
	<updated>2026-05-27T19:44:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://wiki.fintechlab.unibocconi.eu/index.php?title=Cryptocurrency_data_collection_in_python&amp;diff=314&amp;oldid=prev</id>
		<title>3126123: Created page with &quot;In this document, we provide a brief tutorial on using CoinMarketCap Pro API to access cryptocurrency data. We&#039;ll display how to access exchange trading volume and cryptocurre...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.fintechlab.unibocconi.eu/index.php?title=Cryptocurrency_data_collection_in_python&amp;diff=314&amp;oldid=prev"/>
		<updated>2023-02-27T23:13:06Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In this document, we provide a brief tutorial on using CoinMarketCap Pro API to access cryptocurrency data. We&amp;#039;ll display how to access exchange trading volume and cryptocurre...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In this document, we provide a brief tutorial on using CoinMarketCap Pro API to access cryptocurrency data. We&amp;#039;ll display how to access exchange trading volume and cryptocurrency OHLCV data, but using other endpoints is essentially similar. You can find the CMC API documentation [https://coinmarketcap.com/api/documentation/v1/#section/Introduction here].&lt;br /&gt;
&lt;br /&gt;
The only dependencies necessary to run the illustration are &amp;lt;code&amp;gt;load_dotenv&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;requests&amp;lt;/code&amp;gt;.&lt;br /&gt;
[[File:Snip1.png|center|346x346px]]&lt;br /&gt;
Define a class which will contain the logic for accessing the API. The user can either pass &amp;lt;code&amp;gt;api_key&amp;lt;/code&amp;gt; parameter directly to the &amp;lt;code&amp;gt;__init__&amp;lt;/code&amp;gt; method or set it as an environment variable. The implementation supports &amp;lt;code&amp;gt;.env&amp;lt;/code&amp;gt; files as &amp;lt;code&amp;gt;load_dotenv&amp;lt;/code&amp;gt; is called. Make sure to manually create the env file populated with your API key. The base URL is automatically selected based on the existence of a paid API key, but can be overridden if the parameter is set by the user.&lt;br /&gt;
[[File:Snip2.png|center]]&lt;br /&gt;
Add the &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt; property, which persists the API key across all requests. Furthermore, a &amp;lt;code&amp;gt;Session&amp;lt;/code&amp;gt; instance uses a connection pool and reuses the TCP connection, which will lead to a significant increase in performance.&lt;br /&gt;
[[File:Snip3.png|center]]&lt;br /&gt;
Define a private method, &amp;lt;code&amp;gt;__make_req&amp;lt;/code&amp;gt;, which will be used by public methods to send requests to the API. &amp;lt;code&amp;gt;__make_req&amp;lt;/code&amp;gt; receives the endpoint address, a dictionary of parameters, which must comply the format explained in the endoint&amp;#039;s documentation, and the version of the endpoint. If the API returns an error, it&amp;#039;ll be automatically raised. Otherwise, the response will be converted to a &amp;lt;code&amp;gt;dict&amp;lt;/code&amp;gt; object and returned.&lt;br /&gt;
[[File:Snip4.png|center]]&lt;br /&gt;
Time to add the public methods. We&amp;#039;ll define methods for cryptocurrency OHLCV and exchange quotes endpoints.&lt;br /&gt;
[[File:Snip5.png|center]]&lt;br /&gt;
The user can access the data now.&lt;br /&gt;
[[File:Snip6.png|center]]&lt;br /&gt;
The code results in:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;{&amp;#039;status&amp;#039;: {&amp;#039;timestamp&amp;#039;: &amp;#039;2022-09-06T14:55:37.113Z&amp;#039;,  &amp;#039;error_code&amp;#039;: 0,  &amp;#039;error_message&amp;#039;: None,  &amp;#039;elapsed&amp;#039;: 22,  &amp;#039;credit_count&amp;#039;: 1,  &amp;#039;notice&amp;#039;: None}, &amp;#039;data&amp;#039;: {&amp;#039;binance&amp;#039;: {&amp;#039;quotes&amp;#039;: [{&amp;#039;timestamp&amp;#039;: &amp;#039;2022-09-06T14:50:12.000Z&amp;#039;,     &amp;#039;quote&amp;#039;: {&amp;#039;USD&amp;#039;: {&amp;#039;volume_24h&amp;#039;: 73522805073.14716,       &amp;#039;timestamp&amp;#039;: &amp;#039;2022-09-06T14:50:12.000Z&amp;#039;}},     &amp;#039;num_market_pairs&amp;#039;: 1702}],   &amp;#039;id&amp;#039;: 270,   &amp;#039;name&amp;#039;: &amp;#039;Binance&amp;#039;,   &amp;#039;slug&amp;#039;: &amp;#039;binance&amp;#039;},  &amp;#039;kraken&amp;#039;: {&amp;#039;quotes&amp;#039;: [{&amp;#039;timestamp&amp;#039;: &amp;#039;2022-09-06T14:50:11.000Z&amp;#039;,     &amp;#039;quote&amp;#039;: {&amp;#039;USD&amp;#039;: {&amp;#039;volume_24h&amp;#039;: 708319460.386547,       &amp;#039;timestamp&amp;#039;: &amp;#039;2022-09-06T14:50:11.000Z&amp;#039;}},     &amp;#039;num_market_pairs&amp;#039;: 619}],   &amp;#039;id&amp;#039;: 24,   &amp;#039;name&amp;#039;: &amp;#039;Kraken&amp;#039;,   &amp;#039;slug&amp;#039;: &amp;#039;kraken&amp;#039;}}}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Finally, you need to mind the number calls made in a limited interval and potentially add pauses to the code, so the rate limit is not hit. As suggested by CMC, an [[wikipedia:Exponential_backoff|exponential backoff algorithm]] could be used. While in this tutorial we used the CMC API, fetching data from any other API would be quite similar.&lt;br /&gt;
&lt;br /&gt;
The source code for this article can be found [https://github.com/HumanRupert/algorand_handout here].&lt;/div&gt;</summary>
		<author><name>3126123</name></author>
	</entry>
</feed>