Cards and results › A race and its market
A race and its market
Take the latest settled race and read its market: the book percentage and every price, and what moved before the off.
// A race and its market
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)
func get(path string, q url.Values) any {
req, _ := http.NewRequest("GET", "https://api.apihorseracing.com"+path+"?"+q.Encode(), nil)
req.Header.Set("X-API-Key", os.Getenv("AHR_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var body map[string]any
json.NewDecoder(res.Body).Decode(&body)
if res.StatusCode != 200 {
panic(fmt.Sprint(body["error"]))
}
return body["data"]
}
func main() {
first := get("/v1/results/latest", url.Values{"limit": {"1"}}).([]any)[0].(map[string]any)
data := get("/v1/races/"+fmt.Sprint(first["race_id"])+"/market", url.Values{})
out, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(out))
}
What the reply holds
- race_id
- The race.
- book percentage
- The sum of the starting prices as chances.
- runners
- Every runner with its prices.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.