OpenFIGI / api-examples

Examples of programs that interact with the OpenFIGI services via their APIs.
Apache License 2.0
139 stars 50 forks source link

Help using the v2/search endpoint #31

Closed convergenceIM closed 3 years ago

convergenceIM commented 4 years ago

I'm new to the API so apologies for the basic question. I would like to try the search endpoint working and cannot find any example code in this repo or elsewhere online. I had assumed this would just require changing the openfigi_url = line to look like openfigi_url = 'https://api.openfigi.com/v2/search' but that throws an HTTP Error 400: Bad Request message.

How should I use this endpoint? Related, does the search method look inside of all fields or only the ticker field? The example on the openfigi project page only shows matches on ticker.

jayflo commented 4 years ago

Hey @convergenceIM . Thanks for the question. This repo was created before /search existed so there aren't any examples here. You can find the documentation here: https://www.openfigi.com/api#post-v2-search

If you have used the /mapping endpoint, the main difference in the request is that /search does not allow you to send multiple queries at the same time. So you're request will just be an object (rather than array), e.g.:

{
    "query": "ibm",
    "exchCode": "US"
}

Also note that /search allows only 20 requests/min, the result set is paged (100 FIGIs per page) and you can get a max of 100 pages per query. The response of this search will contain the FIGIs and a next property that you can include with the same query in as the start property in order to get the next page, e.g.:

{
    "query": "ibm",
    "exchCode": "US",
    "start": "QW9JSVFEOFMrQ3hDUWtjd01ERTRTMHhhUXpBPSAx.3AG33VCsv54AsUl5fGHehSytWPuWLJxf0t8VL3YXuJh="
}

Please give it a try and let me know.

dsarachaga commented 4 years ago

Hello, I'm facing the same issue, I wanted to know if there's a solution to this. Thanks

Panache1 commented 3 years ago

jayflo's example doesn't work for me either. I also get the 400 error.

byang183 commented 3 years ago

I'm checking, will get back to this ticket

byang183 commented 3 years ago

more information needed, query with jayflo's example does work for me. please reopen this ticket with complete example code.

byang183 commented 3 years ago

Below is the code I used

import http.client

conn = http.client.HTTPSConnection("api.openfigi.com")

payload = "{\"query\": \"IBM\", \"exchCode\": \"US\"}"

headers = { 'Content-Type': "application/json" }

conn.request("POST", "/v2/search", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
Panache1 commented 3 years ago

Here's my full code in C#: ``using System.Collections.Generic; using System; using System.Text; using System.Net; using System.Text.Json;

namespace OpenFIGISearchTest { class Program { static void Main(string[] args) { using (WebClient webClient = new WebClient()) { webClient.Headers[HttpRequestHeader.ContentType] = "application/json";

            // THIS WORKS
            //var webResponse = webClient.UploadData("https://api.openfigi.com/v2/mapping", Encoding.GetEncoding("UTF-8").GetBytes("[{\"idType\":\"TICKER\",\"idValue\":\"IBM\",\"exchCode\":\"US\"}]")); // Post the mapping information as a byte array to the API

            // THIS RETURNS 400 Bad Request
            var webResponse = webClient.UploadData("https://api.openfigi.com/v2/search", Encoding.GetEncoding("UTF-8").GetBytes("[{\"query\":\"IBM\",\"exchCode\":\"US\"}]")); // Post the search information as a byte array to the API

            var jsonData = JsonSerializer.Deserialize<List<OpenFIGISecurity>>(webResponse); // Deserialize the web response
            Console.WriteLine(jsonData[0].data[0].name);
        }
    }
}
public class FIGIData {
    public string figi { get; set; }
    public string name { get; set; }
    public string ticker { get; set; }
    public string exchCode { get; set; }
    public string compositeFIGI { get; set; }
    public string uniqueID { get; set; }
    public string securityType { get; set; }
    public string marketSector { get; set; }
    public string shareClassFIGI { get; set; }
    public string uniqueIDFutOpt { get; set; }
    public string securityType2 { get; set; }
    public string securityDescription { get; set; }
}
public class OpenFIGISecurity {
    public List<FIGIData> data { get; set; }
}

}``

byang183 commented 3 years ago

search api support only single object, mapping api support Array, that's why it works for mapping not for search, @Panache1

// THIS RETURNS 400 Bad Request var webResponse = webClient.UploadData("https://api.openfigi.com/v2/search", Encoding.GetEncoding("UTF-8").GetBytes("[{\"query\":\"IBM\",\"exchCode\":\"US\"}]")); // Post the search information as a byte array to the API

byang183 commented 3 years ago

[{"query":"IBM","exchCode":"US"}] => {"query":"IBM","exchCode":"US"} should make it work

Panache1 commented 3 years ago

Thanks. Any in case anyone looks at this later, the deserializer isn't a list. It should be:

var jsonData = JsonSerializer.Deserialize<OpenFIGISecurity>(webResponse); // Deserialize the web response Console.WriteLine(jsonData.data[0].name);