Issues with a Web API

Hello, I am having trouble with an API from a website. I have added the parameters from the website but it still will not output the information I want. This is the code.

import requests


run_reg_url = "http://www.RunReg.com/api/search"

user_input = input("Enter the event type: ")

params ={
    'eventtype': user_input,
}

#I used get to access the data from the API 
r = requests.get(run_reg_url, params=params)

import json

parse_json = json.loads(r.text)

matching_events = parse_json.get('MatchingEvents', [])


if matching_events:
    print(f"{user_input} data:")
    for event in matching_events:
        print("Event name:", event.get('name', 'Not available'))
        print("Location:", event.get('loc', 'Not available'))
        print("Date:", event.get('date', 'Not available'))
        print("Distance:", event.get('distance', 'Not available'))
        print("Region:", event.get('region', 'Not available'))
else:
    print("No data for marathons found.")

With an alteration to your code:

if matching_events:
    print(f"{user_input} data:")
    for event in matching_events:
        print(f"Event name: {event['EventName']}")
        print(f"Location: {event['EventAddress']}, {event['EventCity']}")

… and an input of fun run, I get this output:

Enter the event type: fun run
fun run data:
Event name: Jingle Jog 5k Fun Run
Location: 1 Daytona Blvd , Daytona Beach
Event name: The Christmas Carroll 5k/ Fun Run
Location: 3320 S 16th St, Chickasha, Chickasha 
Event name: Jingle Bell Jog 2023
Location: 11 Green St, Brattleboro
Event name: Hot Cocoa Run 2023
Location: 515 Keystone Blvd , Patterson
Event name: Halo's 5K Santa Run/Walk
Location: 102 Main St, Barton
Event name: Ugly Sweater 5K and Kids Race at Tehaleh
Location: 17679 Cascadia Blvd E. , Bonney Lake 
Event name: Freeze Your Buns Run
Location: 3200 Mill St, Medina
Event name: THRIVE OUTSIDE FESTIVAL
Location: 1257 County Farm Rd, Bedford
Event name: George's Goats for Grammas Memorial Run/Walk
Location: 1st Street, Cable, WI, Cable
Event name: Runway 5K
Location: 160 Old Ferry Rd, 160 Old Ferry Road
Event name: 7th Annual Huntley Hootenanny 5K Glow Run/Walk
Location: 10987 Main St, Huntley
Event name: Council Bluffs Community 5k
Location: 4200 Avenue B, Council Bluffs

The keys() seem to be:

Categories CoverPhoto Distance EventAddress EventCity EventDate EventEndDate EventId EventLogo EventName EventNotes EventPermalink EventState EventTypes EventUrl EventWebsite EventZip Latitude Longitude Permit PledgeRegUrl PresentedBy RegCloseDate RegOpenDate

I hope this helps.

Yes that is very helpful thank you! Do you know why I am unable to parse the marathon data from the API? Does it only have the fun run data?

You’re welcome.

I’ve just tried that and this is just a snippet of the output I get:

Enter the event type: marathon
marathon data:
Event name: 2023 Galaxy of Lights runs
Location: 4747 Bob Wallace Ave SW, Huntsville
Event name: Club de course à pied Gaspesia - Inscriptions 2023
Location: 37 Rue Chrétien, Gaspé
Event name: Jingle Jog 5k Fun Run
Location: 1 Daytona Blvd , Daytona Beach
Event name: Rochester Runners 2023 Annual Membership
Location: , Rochester
Event name: BASC Santa Claus Shuffle
Location: 700 Elm St., Manchester
Event name: TARC Winter Classic
Location: Fallon Road, Stoneham

It could very well be that I’m not using the API as I should be using it, but with the limited information I have here, it’s just a general idea of what you should be doing. I’m sure that you’ll be able to get the usage info from the website.

Thank you again!