2
$\begingroup$

I have obtained a Yahoo Finance API key and would like to try it out by making a simple request from within Mathematica. Their tutorial page has some nice little example code snippets in various common coding languages but I don't see anything for Mathematica and I haven't made much headway in constructing my own since I don't have prior experience with Mathematica's web services nor API requests in general.

Per their specification page, I think the requisite input will just be a request URL like https://yfapi.net/v6/finance/quote?region=US&lang=en&symbols=GOOGL plus my API key, but I haven't been able to identify the proper built-in Mathematica function (much less the proper syntax) for putting this information to work.

Any hints/pointers would be appreciated. Apologies for the naivety of my query.

EDIT: Using the code provided in the answer by @lericr gives the following output:

enter image description here

$\endgroup$
8
  • $\begingroup$ Please share the code you have tried and the problem you encountered. $\endgroup$
    – bbgodfrey
    Feb 16, 2022 at 19:26
  • $\begingroup$ That's a totally reasonable request...the trouble is that I don't even know what Mathematica function I should be using. I've looked at documentation for Import, HTTPRequest, URLExecute, etc. but something like URLRead["yfapi.net/v6/finance/quote?region=US&lang=en&symbols=\USSPX", {Method -> "GET", Headers -> {"accept: application/json", "X-API-KEY: [MY KEY]"}}] returns "403 Status: Forbidden". I just don't know enough to fail in an instructive way. $\endgroup$
    – user143410
    Feb 16, 2022 at 20:29
  • $\begingroup$ Regarding your update, can you show your HTTPRequest? It worked fine for me, so I need to see if anything was different. And just for future reference, it’s more efficient to show your code than describe it. $\endgroup$
    – lericr
    Feb 16, 2022 at 21:03
  • 1
    $\begingroup$ Well, your code is identical to mine other than the actual api key, and I'm getting a full response. So, this is a bit of a mystery. Maybe there's something in your Yahoo profile that affects this? At least you got a 200 response, so your api key is working. I think it's weird that they return a 200 and then embed an error code, but whatever. A 406 error code suggests that you might need to specify a response format. $\endgroup$
    – lericr
    Feb 16, 2022 at 23:17
  • 1
    $\begingroup$ You could try modifying the header part of the request to look like this: "Headers"->{"x-api-key"->apikey,"Accept"->"application/json"}. Their dashboard seems to always fill in language and region as query params, so you might try adding those to the "Query" part of the request. You might try opening up a terminal and running the curl command shown on the Yahoo finance dashboard. If that also "fails", then we would know that it has nothing to do with Mathematica. $\endgroup$
    – lericr
    Feb 16, 2022 at 23:18

2 Answers 2

3
$\begingroup$

Here's one way to get there:

(*create a request structure*)
req = 
  HTTPRequest[
  "https://yfapi.net/v11/finance/quoteSummary/AAPL", 
  <|"Headers" -> {"x-api-key" -> "YOUR KEY HERE"}, 
    "Query" -> {"modules" -> "defaultKeyStatistics"}|>]

(*make the call to the service and store the response*)
response = URLRead[req]

(*inspect the response--Body is just one of many properties you can look at*)
response["Body"]

The documentation for HTTPRequest, URLRead, and HTTPResponse might seem pretty dense, but it is helpful and has some good examples.

$\endgroup$
2
  • 1
    $\begingroup$ BTW, I discovered the modules query param and its possible values in the Yahoo documentation you linked to. I don't know if that's the data you want. Also, just in case it gets lost in the code, you need to replace "YOUR KEY HERE" with your actual key. And you should probably not post your key anywhere--can you edit/delete your comment above? $\endgroup$
    – lericr
    Feb 16, 2022 at 20:27
  • 1
    $\begingroup$ Thank you for your helpful response. I think it has moved me forward: It returns a status of "OK" now so it appears that it is accepting my api key. However, I'm still quite confused about the output from response["Body"] (see edit to my question) or the output from just response[] $\endgroup$
    – user143410
    Feb 16, 2022 at 20:58
1
$\begingroup$

Alternatively, you could try the following:

URLFetch["https://yfapi.net/v11/finance/quoteSummary/AAPL", 
 "Method" -> "Get", 
 "Headers" -> {"X-API-KEY" -> 
    "YourKeyHere"}, 
 "Parameters" -> {"modules" -> "defaultKeyStatistics,assetProfile"}]

It should output the asset profile of Apple. You can change the parameters according to Yahoo Finance API to get the specific items you are looking for.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.