Usage for API to create observations

Good evening,
I’m asked for creating a web page for my client (Maremma Natural History Museum) where users can submit their observations. In past we used the Indicia solution but we had problems.
I can retrieve the API token and the Access Token for a user but which is the best set of API to use, the “/v1” or the other? Is there any example where I can understand and learn how to submit an observation?
Thanks

1 Like

here’s a very simple snippet in Ruby to allow an iNat user to authorize you to post an observation on their behalf. If you’re going to also create iNaturalist users, please allow them to create individual accounts (rather than post to group a group account)

####
require 'rubygems'
require 'rest_client'
require 'json'

# First, make an application here https://www.inaturalist.org/oauth/applications/new
#and note your app_id, app_secret, redirect_uri below, and also the Authorize' url

site = "https://www.inaturalist.org"
app_id = 'xxxxxxxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxxxxxxx'
redirect_uri = 'http://www.example.com'

# Next, have your users visit the 'Authorize' url in a browser browser while logged in
# and handle the auth code

auth_code = "xxxxxxxxxxxx"

# Next, use this auth_code and the above info to get a token for the user

payload = {
  :client_id => app_id,
  :client_secret => app_secret,
  :code => auth_code,
  :redirect_uri => redirect_uri,
  :grant_type => "authorization_code"
}
response = RestClient.post( "#{site}/oauth/token", payload )
token = JSON.parse( response )["access_token"]
headers = { "Authorization" => "Bearer #{token}" }

# Now make a observation on behalf of the user

results = RestClient.post( "#{site}/observations.json",{
  "observation[species_guess]" => "Northern Cardinal",
  "observation[taxon_id]" => 9083,
  "observation[observed_on_string]" => "2013-01-03",
  "observation[time_zone]" => "Eastern Time (US %26 Canada)",
  "observation[description]" => "what a cardinal",
  "observation[tag_list]" => "foo,bar",
  "observation[place_guess]" => "clinton, ct",
  "observation[latitude]" => 41.27872259999999,
  "observation[longitude]" => -72.5276073,
  "observation[map_scale]" => 11,
  "observation[location_is_exact]" => false,
  "observation[positional_accuracy]" => 7798,
  "observation[geoprivacy]" => "obscured"
}, headers )

puts "created https://www.inaturalist.org/observations/#{ JSON.parse( results )[0]["id"] }"
2 Likes

Thanks for your quick reply!
The idea is let users register and create their account (I tried using my google account and social login) and then create a page with a very simple form with the name of what thet observed, a map and few other info. I thoght all client side if possibile (or using a php proxy called from UI). I cannot use Ruby, only PHP and JS (I cannot use neither nodeJS, it’s a shared hosting)

I recommend you look at the observations POST endpoint at https://api.inaturalist.org/v1/docs/#!/Observations/post_observations . If you go there, you’ll notice there is a red (i) that allows you to put in your JWT (see the top of the docs) and you can test POSTing a new observation from within the documentation. It expects a JSON body formatted like the example, and you can send any attributes listed in Scott’s example above.

Just be aware that the test calls will actually be run against the production database, so be sure to either test with real data, or include a statement in the observation description that it is a test and eventually clean up.

As for the actual implementation - if you want to do it with JavaScript you might find https://github.com/inaturalist/inaturalistjs#creating-records helpful, or if you’re using PHP you could leverage curl (our API documentation will give you an example curl command-line command you could migrate to PHP).

2 Likes

Hi!
I tried with the POST v1/post_observations and from Swagger all ok and posted correctly the observation.
To connect with a project, have I to use the https://api.inaturalist.org/v1/docs/#!/Project_Observations/post_project_observations ?
And also to add a photo, if any, with the /observations_photo in formdata with ID and UUID from the saved observation?
Thanks

I have finally done an observation by API from PHP with image and associating it to a project.
How can I set if what I have seen is male/female or if it is in captivity?
Is there an API to send audio file or is there only image?
Thanks