Results 1 to 12 of 12

Thread: Free stock API?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Question Free stock API?

    Since many of the Windows 7 desktop gadgets no longer work, I wrote one to show me the current prices of my stock holdings. I use the twelvedata.com API for the data and my program is configured to access the API not more than several hundred times per day (which assumes my computer is on for the entire active stock day), so it is not terribly demanding.

    The API worked without issue for several years, but a while ago, it stopped showing the value for the indices DJI, IXIC, and SPX. Today (and I checked this by entering the API URL into my browser), the twelvedata.com API indicated that these index strings are not valid.

    Question: Can anyone suggest a free API that will provide the current-ish value for these indices about 50 times per day? Not sure that this is the proper forum in which this question should be asked, and if you have any ideas of better forums where this can be asked, please share.

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    631

    Re: Free stock API?

    according to Perplexity AI, these are options:

    A good free option is **API Ninjas Stock Price API**: it supports market index prices, including `^DJI`, and free users get 15-minute delayed data, which is usually enough for “current-ish” values about 50 times per day. If you need all three symbols in a single, simple feed, **Twelve Data** is another strong choice because it supports global indices with updates every 1 minute, though its free plan details are more constrained than the paid tiers. [api-ninjas](https://api-ninjas.com/api/stockprice)

    ## Best fit
    - **API Ninjas**: simplest if you want `^DJI`, `^IXIC`, and `^SPX`-style index lookups and can tolerate delayed quotes on the free plan. [api-ninjas](https://api-ninjas.com/api/stockprice)
    - **Twelve Data**: better if you want broader index coverage and more frequent updates, with an indices API designed for real-time/historical pricing. [twelvedata](https://twelvedata.com/indices)
    - **Alpha Vantage**: worth considering for general market data, but it is not the clearest match here for direct U.S. index quotes from the sources I checked. [alphavantage](https://www.alphavantage.co)

    ## Practical note
    For 50 requests per day, rate limits are usually not the issue; the bigger question is whether the provider returns the exact index symbols you want versus requiring a slightly different ticker or a related proxy such as an ETF. For your use case, I’d start with **API Ninjas** and verify that it returns the three indexes in the format you need. [twelvedata](https://twelvedata.com/indices)

    ## Symbol caveat
    Different APIs use different tickers for these indexes. In the sources I found, examples included `^DJI` for the Dow and `^GSPC` for the S&P 500, while Nasdaq Composite coverage depends on the provider’s symbol mapping. [financialdata](https://financialdata.net/index-quotes-api)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Re: Free stock API?

    Thanks. I will check out API Ninjas - am currently using Twelve Data and it does not provide the desired info.

  4. #4
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    631

    Re: Free stock API?

    You're welcome, hopefully it works out for you.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Re: Free stock API?

    Dumb question alert! Is there some way to pass the API_KEY via the url? Id not, would someone please point me to an example of how to pass the API_KEY.

    Thanks.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,388

    Re: Free stock API?

    According to API Ninja's documentation (link), you need to set the X-Api-Key header.

    Here is an untested example:
    Code:
    Public Async Function GetStockPrice(ticker As String) As Task(Of String)
        If (String.IsNullOrWhitespace(ticker)) Then
            Throw New ArgumentOutOfRangeException(NameOf(ticker))
        End If
    
        Dim baseUrl As String = "https://api.api-ninjas.com/v1/stockprice"
        Dim query As NameValueCollection = HttpUtility.ParseQueryString($"ticker={ticker}")
        Dim url As String = $"{baseUrl}?{query.ToString()}"
        Dim apiKey As String = "CHANGE ME"
    
        Using client As New HttpClient()
            Using request As New HttpRequestMessage(HttpMethod.Get, url)
                request.Headers.Add("X-Api-Key", apiKey)
    
                Using response As HttpResponseMessage = Await client.SendAsync(request)
                    response.EnsureSuccessStatusCode()
    
                    Dim responseBody As String = Await response.Content.ReadAsStringAsync()
                    Return responseBody
                End Using
            End Using
        End Using
    End Function
    Last edited by dday9; Yesterday at 09:25 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Re: Free stock API?

    In late April, I wrote a simple program to test the API-Ninja API and it worked without issue. However, as of last week when I try running the exact same program, it gives me an error: Response status code does not indicate success.

    Did API-Ninja's stockprice API change? The API is failing when I try to get the DJI price and it symbol, for the API, is ^DJI.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,388

    Re: Free stock API?

    I tested the function in post #6 using my API key and ^DJI and it worked just fine. Please note that I forgot the "New" keyword in my original response when throwing the exception, so I edited it to include it.

    What is the status code itself (e.g. 400, 401, 403, etc.)? That will tell us a lot about the issue.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Re: Free stock API?

    dday9 - Thanks for the reply. To make things simple for testing purposes, I added the extension ModHeader to Supermium (basically Chrome) and I added my API_Key to the headers. A couple of weeks ago this worked just fine, but when I tried it again today, using this:
    HTML Code:
    https://api.api-ninjas.com/v1/stockprice?ticker=^DJI
    as the URL, I was informed
    This page isn’t working
    If the problem continues, contact the site owner.
    HTTP ERROR 400
    But when I replaced ^DJI with a stock symbol, it worked fine.

    What on earth is going on? Do the indices require a paying account?
    Last edited by groston; Yesterday at 10:24 AM.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,388

    Re: Free stock API?

    No, because I could get it just fine. Are you properly escaping the query string parameter since the top hat character is a symbol?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    164

    Re: Free stock API?

    Just figured this out. The proper URL to use is: https://api.api-ninjas.com/v1/stockprice?ticker=%5EDJI
    Last edited by groston; Yesterday at 02:33 PM.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,388

    Re: Free stock API?

    Yep, that is what I meant by "properly escaping" the URL.

    This strongly suggests that you're not using the function I originally provided you because in that function, I'm doing that with the HttpUtility.ParseQueryString method.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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