Results 1 to 16 of 16

Thread: Cryptocurrency API

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    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.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    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."

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    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."

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    Re: Cryptocurrency API

    By the way, here is a link to their API documentation: https://coinmarketcap.com/api/documentation/v1/#

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    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?

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    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)

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: Cryptocurrency API

    Just a guess, but try adding the “New” keyword in front of “ HttpUtility.ParseQueryString”. What happens then?

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,677

    Re: Cryptocurrency API

    Quote Originally Posted by Peter Swinkels View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: Cryptocurrency API

    I see. @DreamWeave: has the error been fixed now?

  12. #12
    New Member
    Join Date
    Jul 2021
    Posts
    1

    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

  13. #13
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: Cryptocurrency API

    Wonderful! You're welcome!

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    6

    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!

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,677

    Re: Cryptocurrency API

    Quote Originally Posted by RyanmPooleq View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    New Member
    Join Date
    Oct 2024
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width