Select observations to batch-download from list of observation IDs

as far as i know, the rinat library hits the deprecated API. so i personally would not develop against that. instead, i would hit the current version of the API (currently, v1) directly or else use some module that hits the current API, such as spocc.

it depends on the capabilities of module you’re using. if you’re hitting the API directly, definitely yes.

for me, usually the easiest thing to do when hitting the API directly is to go to https://www.inaturalist.org/users/api_token while you’re logged into inaturalist.org. you’ll see a bit of text that looks something like {"api_token": "api.token.value"}. this is a JSON web token (JWT). i’ll copy the api.token.value and use this as my bearer token in my Authorization header when i make my API requests. the JWT will expire every 24 hours. so you’ll just need to get a new one if you want to do the same thing after it has expired. you don’t want to share the JWT with anyone else or store it in a way that is insecure because anyone who has access to it will be able to do have full access to your account until the token expires.

if you want to have a more automated or secure workflow, it’s a more complicated setup. you’d have to go through the process of registering an iNaturalist application and then set up a flow to get an OAuth token and then a JWT using the OAuth token. (technically it’s possible to use the OAuth token to make some requests in place of the JWT, but it’s not something i would do because i believe iNat doesn’t expire the OAuth token until you request another, and it could be a bigger deal if you lost control of it unknowingly than losing control of a JWT.)

it’s not R, but if you want to avoid having to write something from scratch, you’re welcome to use my JupyterLite workbook to get observation data from the API. it runs a version of Python that can be run via JupyterLite directly in your browser, which can save you the work of setting up a special environment just to run scripts.

When running from JupyterLite, all your requests happen from your machine, and your changes and results get saved in your browser’s storage, which can then be downloaded to your machine, if you like.

in its simplest usage that includes user authorization, you would:

  1. open the workbook via JupyterLite in your browser.
  2. in the line that begins jwt = , set this equal to your JWT value (ex. jwt = 'api.token.value').
  3. in the line that begins req_params_string = , set the value equal to whatever parameters you would like to apply in your request. if you’re trying to specify a list of obs IDs, it would be something like req_params_string = 'id=10000,10001,10002'
  4. in the line that begins with obs = await, you would set get_all_pages=True if you expect to get more than 30 observations, and use_authorization=True
  5. in the section that begins with parse_fields = [, define which fields you want to get in your results. i’ve pre-defined a few fields that i thought might be generally useful. so you might be able to just uncomment or comment the fields you’re interested in or not interested in. in cases where a field might have multiple values per observation, i’ve defined most of these to flatten the values (string multiple values together into one string) or else provide some aggregate result such as a count. you’re welcome to adapt these definitions in case you need something that hasn’t been pre-defined.
  6. then run each cell from the top, or run all. in your case, you want the results in a CSV. so you don’t have to run anything past the “Write Data to CSV” section. the resulting CSV file will be stored in browser storage, and you’ll be able to see it in the JupyterLite file navigation tree on the left side of the page. you can click on it to view it in the JupyterLite interface, or you can right-click and download the file to your machine so that you can work with it in your own applications on your machine.

any changes to the workbook you make should get automatically saved along the way in browser storage as you run things or as you click the save button. if you want to preserve the original workbook so that you can start over easily, it’s cleanest if, before step 1 above, you find the file in the file navigation tree and save a new version, and then work in that new version. your work will be preserved across browsing sessions as long as you don’t clear your storage. or if you do all your work in a private browsing session, everything will always reset back whenever you start a new private browsing session. just remember that you’ll want to download any workbooks to your machine if you want to preserve them long-term.

2 Likes