Deohgu
Deohgu

Reputation: 55

How do I query information from the Trefle API

I didn't want to ask such a basic question but I seem to not be able to find the answer on my own.

How can I query a specific plant without knowing its id? Such as using the common name or binomial name. I see that according to the documentation the path is /api/species/{id}, it might be my inexperience on using APIs but I'm left clueless on how to query a specific plant without prior knowledge of their id.

Would anyone be able to give me an explanation of how it works or even better link me to an article to fill the gaps of my API knowledge? My current knowledge stems from the freecodecamp JSON APIs and AJAX short course which doesn't help when faced with this sort of documentation.

Upvotes: 1

Views: 1543

Answers (1)

Deohgu
Deohgu

Reputation: 55

I got the response from another forum and then complemented it by learning more about REST APIs - APIs for Beginners - How to use an API (Full Course / Tutorial)


At the basic level in the form of a URL:

  1. You start with the provided URL - https://trefle.io/api
  2. Select which type of data you want such as Kingdom, subKingdom, division, etc All the way down to plants or species. - https://trefle.io/api/plants. As it is mentioned, "Plants are all main species, without all the varieties, cultivars, subspecies and forms", and species will give you all that belongs to that species meaning several plants.
  3. You provide the parameters that you want, with some being required and others being optional. To know which parameters to use you can read underneath here, such as token, page_size, page, etc. The first parameters would go in front of the former link like so with ? - https://trefle.io/api/plants?q=strawberry and the following parameters are separated with a & like so - https://trefle.io/api/plants?q=strawberry&token=yourAPIkey and you can add any parameters to that such as showing only items with complete data, minimum ph, or others that might interest you. But do keep in mind that the database still seems to be particularly incomplete.

Also, this is in the form of a URL, in practice, this would be done differently, I recommend watching the video I linked above where he goes through a lot of this information and showcases helpful tools such as Postman.

Upvotes: 1

Related Questions