Results 1 to 11 of 11

Thread: Cryptocurrency API

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    5

    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
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    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
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    5

    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
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    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.
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    5

    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
    5

    Re: Cryptocurrency API

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

  7. #7
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    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?
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    5

    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
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: Cryptocurrency API

    Just a guess, but try adding the “New” keyword in front of “ HttpUtility.ParseQueryString”. What happens then?
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,019

    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
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: Cryptocurrency API

    I see. @DreamWeave: has the error been fixed now?
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

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