Filter the results from an API search

I’ve being tryng to make a command that uses this Tarot API:
https://rws-cards-api.herokuapp.com/

the api itself → https://rws-cards-api.herokuapp.com/api/v1/cards

I want to make something like !tarot Death and get the “meaning_up” from the result of the urlfetch https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=Death
but I don’t know hot to get only that field to show up as a response from the command.

I’ve tried things like this:

!tarot $(eval const resp = $(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring)); resp[1] )

or things that look like this

$(eval JSON.parse(decodeURIComponent($(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring).cards.0.meaning_up||`Error!`)

but I still can’t figure out how to do it.
This is my first time trying to do that kind of commands using jsons, APIs and urlfetch
I’ll appreciate any advice in the right direction

1 Like

Hey @bllq21!

You were kinda close with your second example of what you tried, but when the key is a number or a string containing anything else than letters and underscores, use square brackets []: [2] or ['hello-world']

Without further ado, here’s how you reach the meaning_up key:

$(eval const response = $(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring)); response.cards[0].meaning_up)

And with some error handling:

$(eval const response = $(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring)); '$(query)' ? response.cards && response.cards.length > 0 ? `${response.cards[0].name}: ${response.cards[0].meaning_up}` : 'API response incomplete.' : 'Please specify which card you want to know the meaning of.')
2 Likes

Thank you!
I really appreciate your help, I could’t figure out how to do that part.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.