-
Aug 23rd, 2020, 10:32 PM
#1
Thread Starter
New Member
Cryptocurrency API
I signed up for a free tier API service with CoinMarketCap.com, to be able to quickly get the valuation of some cryptocurrencies I own. As it is, I use a web browser control to load up their page and go through a parsing process to extract the data I need, mostly the current values of a handful of their top 100 coins. But I discovered they offer a free API for personal use, and their API documentation has many examples of various languages. I can only code in Visual Basic, having been on this language since VB4. I tried to convert their C# example into VB, but the function they document fails in my VS2017 project, as I don't really understand the process or how to correctly integrate their C# sample into my VB.NET. If someone would kindly assist with understanding how I need to code this process?
Here is their C# example:
Code:
using System;
using System.Net;
using System.Web;
class CSharpExample
{
private static string API_KEY = "b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c";
public static void Main(string[] args)
{
try
{
Console.WriteLine(makeAPICall());
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
static string makeAPICall()
{
var URL = new UriBuilder("https://undefined/v1/cryptocurrency/listings/latest");
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString["start"] = "1";
queryString["limit"] = "5000";
queryString["convert"] = "USD";
URL.Query = queryString.ToString();
var client = new WebClient();
client.Headers.Add("X-CMC_PRO_API_KEY", API_KEY);
client.Headers.Add("Accepts", "application/json");
return client.DownloadString(URL.ToString());
}
}
============================================
And here is the conversion from Telerik Code Converter:
Code:
Imports System
Imports System.Net
Imports System.Web
Class CSharpExample
Private Shared API_KEY As String = "b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c"
Public Shared Sub Main(ByVal args As String())
Try
Console.WriteLine(makeAPICall())
Catch e As WebException
Console.WriteLine(e.Message)
End Try
End Sub
Private Shared Function makeAPICall() As String
Dim URL = New UriBuilder("https://undefined/v1/cryptocurrency/listings/latest")
Dim queryString = HttpUtility.ParseQueryString(String.Empty)
queryString("start") = "1"
queryString("limit") = "5000"
queryString("convert") = "USD"
URL.Query = queryString.ToString()
Dim client = New WebClient()
client.Headers.Add("X-CMC_PRO_API_KEY", API_KEY)
client.Headers.Add("Accepts", "application/json")
Return client.DownloadString(URL.ToString())
End Function
End Class
==================================
According to their API documentation, they have a sandbox for testing the APIs, so I am guessing the correct address is :
Dim URL = New UriBuilder("https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest")
I signed up for and received my API key, while the sample key above is just that, a sample. So I have the sub and function and Private Shared API_KEY As String as converted, added into my project, but am at a loss on how to call the sub or function.
Last edited by DreamWeave; Aug 25th, 2020 at 01:08 AM.
-
Aug 24th, 2020, 07:40 AM
#2
Re: Cryptocurrency API
Hello Dreamweave, welcome to the forum!
Could you explain how your code fails? Also, please use code tags to enclose your code. You can create those using the "#" button in the toolbar above the message you are about to post.
yours,
Peter Swinkels
-
Aug 24th, 2020, 08:27 AM
#3
Thread Starter
New Member
Re: Cryptocurrency API
Hi Peter, thanks for the advice on code formatting. Wish I could edit the original.
There's no errors in placing the conversion into my project. My problem is with understanding how to use it. I tried to add a button to call the Main sub, but there's the (ByVal args as String()) in that sub, and I tried using "Call Main()" and it needs an argument, saying "Argument not specified for parameter 'args' of the Public Sub Main . ." etc. When I try to add any string in double quotes, such as Call Main("1") it says "Type of 'String' cannot be converted to 'String()'".
On closer inspection, the Sub Main does have grayed underline under the word 'args' and the popup says "Remove unused parameter 'args' if it is not part of the shipped public API". I've tried removing the args parameter last night, and then the process failed in the function "Dim queryString . . ." saying "Object variable or With block variable not set."
-
Aug 24th, 2020, 09:06 AM
#4
Re: Cryptocurrency API
There should be an "Edit Post" link in the lower right corner of your posts while you're are logged in. You shouldn't call Main using a button. Main is used as an entry point for your program you should forget about it. Try doing the following:
1. Remove your Main procedure.
2. Put "makeAPICall" in your button's click event handler.
3. Forget the args parameter. See point 1.
-
Aug 25th, 2020, 01:12 AM
#5
Thread Starter
New Member
Re: Cryptocurrency API
In the button I have:
Code:
Dim thisURL As String = makeAPICall()
. . . and the code still errors on the queryString line within the function, saying "Object variable or With block variable not set."
-
Aug 25th, 2020, 01:48 AM
#6
Thread Starter
New Member
Re: Cryptocurrency API
By the way, here is a link to their API documentation: https://coinmarketcap.com/api/documentation/v1/#
-
Aug 25th, 2020, 03:50 AM
#7
Re: Cryptocurrency API
Do you mean this line: Dim queryString = HttpUtility.ParseQueryString(String.Empty) ? If so, do you know you're asking for an empty string to be parsed? Please post the code in your button and mark the exact line causing the error, okay?
-
Aug 25th, 2020, 07:49 AM
#8
Thread Starter
New Member
Re: Cryptocurrency API
I receive the same error with or without any string in the button:
Code:
Dim thisURL As String = makeAPICall("ETH")
. . . and the error occurs in this line:
Code:
Dim queryString = HttpUtility.ParseQueryString(String.Empty)
-
Aug 25th, 2020, 07:56 AM
#9
Re: Cryptocurrency API
Just a guess, but try adding the “New” keyword in front of “ HttpUtility.ParseQueryString”. What happens then?
-
Aug 25th, 2020, 07:57 AM
#10
Re: Cryptocurrency API
Originally Posted by Peter Swinkels
Do you mean this line: Dim queryString = HttpUtility.ParseQueryString(String.Empty) ? If so, do you know you're asking for an empty string to be parsed? Please post the code in your button and mark the exact line causing the error, okay?
Parsing an empty query string should be fine... that should simply initialize the queryString variable, which is supposed to be a NameValueCollection object.... my guess is that it errors on these lines:
Code:
queryString("start") = "1"
queryString("limit") = "5000"
queryString("convert") = "USD"
Likely because someone didn't read the documentation.
ParseQueryString - https://docs.microsoft.com/en-us/dot...ew=netcore-3.1 <-- this tells us that the returned object is a NameValueCollection
NameValueCollection - https://docs.microsoft.com/en-us/dot...ew=netcore-3.1 <-- here we find the documentation on it, and a VB sample that shows how to add name/value pairs to the collection.
You need to .Add your name/value pairs to the collection...
Code:
queryString.Add("start", "1")
queryString.Add("limit", "5000")
queryString.Add("convert", "USD")
-tg
-
Aug 25th, 2020, 09:08 AM
#11
Re: Cryptocurrency API
I see. @DreamWeave: has the error been fixed now?
-
Jul 13th, 2021, 12:33 PM
#12
New Member
Re: Cryptocurrency API
I tried to do almost the same thing, and by the way, I also wrote it in C#. Adding the “New "keyword in front of" HttpUtility.ParseQueryString " really helped me, so thank you very much, Peter Swinkels!
Last edited by si_the_geek; Jun 12th, 2023 at 11:53 AM.
Reason: removed unnecessary link
-
Jul 13th, 2021, 01:06 PM
#13
Re: Cryptocurrency API
Wonderful! You're welcome!
-
Jul 13th, 2021, 05:49 PM
#14
Thread Starter
New Member
Re: Cryptocurrency API
So sorry for the late reply. Yes, I was finally able to understand the use of the API.
Code:
strReturn = ""
Dim AC_json As String = ""
Try
AC_json = makeAPICall_GET_COINQUOTES()
Catch
End Try
If AC_json <> "" Then
strReturn = AC_json
End If
And here is the function I came up with for getting quotes:
Code:
Private Shared Function makeAPICall_GET_COINQUOTES() As String
Dim URL = New UriBuilder(" https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest")
Dim queryString = HttpUtility.ParseQueryString(String.Empty)
queryString("id") = "1,1027,1831,74,1958,2010,52,1321,5994"
queryString("convert") = "USD"
URL.Query = queryString.ToString()
Dim client = New WebClient()
client.Headers.Add("X-CMC_PRO_API_KEY", API_KEY)
client.Headers.Add("Accepts", "application/json")
Return client.DownloadString(URL.ToString())
End Function
Then it was simply a matter of parsing the return. Thanks for your assistance!
-
Jun 12th, 2023, 11:13 AM
#15
Re: Cryptocurrency API
Originally Posted by RyanmPooleq
Hey there! So, I had a similar situation when I signed up for CoinMarketCap.com's free tier API service. I also tried converting their C# example into VB, but got stuck. Any help with coding this process would be awesome! Thanks!
Start a new thread with the relevant info and code ... for help with that, check out how to remove eels from your hovercraft in my signature link ... key points: post hte relevant code, post relevant data, note what the _expected_ outcome is to be, and what the _actual_ result was. If you supply screenshots, also be away that the forum does some funky stuff with it ... and it's preferrable to just post the text instead (expecially in the case of exception messages)...
-tg
-
Oct 4th, 2024, 06:56 PM
#16
New Member
Re: Cryptocurrency API
I once tried to use an API in a project and ended up getting lost in the code. It took me a while to realize that sometimes the easiest way is to break things down step by step.
For your situation, I found that starting with the basics—like just making a simple API call and printing the result—helps a ton. If you’re stuck with the conversion, you might want to look at how the API structures its requests in VB.
Also, I discovered https://stealthex.io , and they offer some clear documentation too. It’s a great place to see how other developers are working with crypto APIs.
Last edited by Mohawkly; Oct 7th, 2024 at 04:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|