Go (golang) client library for iNaturalist API

Hello, I am starting work on a client library for the iNaturalist API written in Go. I have worked on the Go team at Google since 2012 and have recently gotten engaged with eBird and iNaturalist. I’d like to write some tools to help me manage my observations. My intent is to use this thread to ask questions about the iNaturalist API and solicit feedback from interested parties.

First question: Which API version should I build against? Options:

Second question: is there a machine-readable file describing the API?

I can find the OpenAPI spec for the old (pre-V1?) API: https://github.com/inaturalist/iNaturalistAPI/tree/main/openapi/schema/request

And I’m guessing this is V2: https://github.com/inaturalist/iNaturalistAPI/tree/main/openapi/paths/v2

But I can’t find V1.
I’d like to use this to automatically generate the Go OpenAPI client libraries.

3 Likes

i haven’t looked much at v2, but in most cases, it seems to function more or less the same as v1, except:

  • prioritization of uuid over id as key
  • you can specify which fields you want returned

it looks like v2 is still considered beta, but they already built the new app using v2 (i think), and they’re now in the process of moving much of the website to v2. so for me, i would build on v2, unless you want to be able to do a lot of development using, say, /v1/observations/{id} over /v2/observations{uuid}. (if needed, maybe the way to access something like blah/{id} where something like /v2/blah?id={id} is not available would be to build on v2 + /v1/blah/{id}?)

i thought this was v1, but i could be wrong.

2 Likes

V1 is hardcoded swagger json as ejs view:

https://github.com/inaturalist/iNaturalistAPI/blob/352eba6d283ee79f700e4a7a74ddb1f1b2e83aae/lib/views/swagger_v1.yml.ejs


For the ease of mind I would go with v2. I had never issues with v2 (only with pagination but that is somewhat fixed … better error message).

PS: I love Golang! Looking forward to your modules.

2 Likes

Thanks all! Follow up question. For a tool I’m writing, I’m using some of the custom observation defined on https://www.inaturalist.org/observation_fields

I’d like to be able to fetch some of those fields via the API, but I can’t seem to get the requests to work. For example, this request asks for my personal observations, returning species_guess, observed_on, and the observation field “count” (ID #1) using “field:count” as the field name. It fails with error 422, “invalid fields parameter”:
https://api.inaturalist.org/v2/observations?user_id=sameerajmani&order=desc&order_by=created_at&fields=species_guess%2Cobserved_on%2Cfield%3Acount

Do you know how I can request specific observation fields be included in the response? This is possible in the web UI, so I expect the API supports it with the right arguments.

Thanks!
S

can you provide a screenshot of what you mean when you say it’s “possible in the web UI”?

i know you can filter observations by field or field+value, but that’s different from specifying the fields to be included in the API response.

since observation fields are returned in an ofvs array, i’d be surprised if there was a way to pull a particular item from that array in a request… but if there is a way to do this, i’d be interested in knowing what that that syntax is, too.

at best, i think you would just filter for observations that contain an observation field of interest, and then include ofvs in the API response (ex. https://api.inaturalist.org/v2/observations?per_page=10&field:Count&fields=species_guess,observed_on,ofvs.id,ofvs.name,ofvs.value). from there, you’d probably have to add your own code to pull out the specific item from the ofvs array.

Sure, screenshot attached. I’ll note that field:count and field:ebird only appeared in the export UI after I set those fields on some of my observations via the web UI then exported my observations, IIRC.

i don’t think the CSV export is really tied to the API, or if it is, it’s doing some additional processing on top of what the API returns. note that the CSV also has options for place_xxx_name and taxon_xxx_name fields which aren’t returned in the GET /observations response. in these cases (place name, specific taxon rank name, and observation fields), i think you need to do your own processing to get proper values from the API.

Right, I’m trying to use the API and coax it into returning those additional fields. Thankfully iNaturalist is open source so hopefully I can just track down the export UI code :wink:

i don’t understand how tracking down the export UI code helps you. if you want to pull a particular observation field value, most likely, you need to implement your own code to do that.

if you want to create a flattened result from a JSON response that includes nested objects and arrays, that’s something you need to define, since there are many ways to flatten such things.

Hmm, I seem to be misunderstanding something basic here. I’m fine implementing custom parsing code, but what I’m struggling with is getting the API to return the fields I need. Let’s use this example:
https://api.inaturalist.org/v2/observations?user_id=sameerajmani&order=desc&order_by=created_at&fields=species_guess%2Cobserved_on
This query works and returns uuid, species_guess, and observed_on for each of my personal observations.
What I’m trying to figure out is what I can append to this URL to get the response to include the count observation field. I believe this is possible because the export page is able to do it, but I haven’t looked at the code yet.
Am I mistaken?
S

if you find that this is possible, let me know.

as i noted before:

Aha, thanks for clarifying. I misunderstood your earlier response. I’ll try that and report back, thanks!

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