How to edit values using Kobo API?

Hi, I want to know if it is possible to edit data in Kobo forms through the API and if so, what documentation do you recommend me to know the API calls through Python 3. Thank you very much.

Hi @Akratos, this is possible :+1: Please refer to the documentation here:

https://{kf_url}/api/v2/assets/{asset_uid}/data/#bulk-updating-of-submissions

2 Likes

Thanks Josh. Please, Could you give me an example on python 3? I want to modify multiple records and it is very slow to do it using web scraping techniques.
I only need a simple code to know the process.

Hi @Akratos, you can do something like this in Python:

import requests
import json

URL = 'https://kf.kobotoolbox.org/api/v2/assets/admH7NjjWAEUfqnJfFbfdQ/data/bulk/'
TOKEN = 'your_secret_token'
PARAMS = {
    'fomat': 'json'
}
HEADERS = {
    'Authorization': f'Token {TOKEN}'
}

payload = {
    'submission_ids': ['1111', '1112'],
    'data': {'group_name/question_name': 'new_value'}
}

res = requests.patch(
    url=URL,
    data={'payload': json.dumps(payload)},
    params=PARAMS,
    headers=HEADERS
)

Note that you need to include the full node path to the question, so if the question name is Q1 nested in group G1, you will have the path G1/Q1.

2 Likes

Thanks Josh! I’ll probe your script and give you feedback in short time. This will very usefull for my humanitarian work.
Greetings.

1 Like

Hi @Josh, about your script:

  1. I don’t have a variable called “submission_ids” so I imagine you mean the variable “_id”, is that correct?
  2. Regarding “group_name / question_name” I have questions that do not belong to any group, so how do I refer to these variables or questions? Also, in the questions that are grouped I can get multiple records (eg activities) in a single group / form. What variable should be used in that case? _Index, _parent_index?
  3. I have done the test with your script, I print the variable ‘res’ and I get <Response [403]> I clarify that I use my TOKEN and the form I have created.
  4. Assuming that everything turns out well, what command should be used to edit the registry, that is, change one value to another. This is the main problem why I opened this topic.
    Thank you very much for your valuable help.

Hi @Akratos,

  1. That’s correct
  2. If a question is outside of a group, you just reference it as question_name instead of group_name/question_name. Unfortunately editing of repeat groups through the API is not yet supported. You can edit those manually still outside of the application in a script but this is far more involved.
  3. Correct. You will need to use your account token (or whatever user token that has permission to edit submissions for a particular project).
  4. The PATCH request in the above script is the step of changing an old value to a new value specified in the payload.

You can look in the network tab in your browser’s developer tools to see how this is done from the UI as you make an edit. Here’s a short screen recording of what that looks like.

1 Like

Thank you very much @Josh!
Problem solved. I have connected as you indicate and I have managed to edit the data from Python.
I would only modify the submission_ids in your script so that anyone understands that it is an id per record and if someone wants to iterate through for loop, then do it as you indicated. I would leave that script line like this:

payload = {‘submission_ids’: [‘239564975’],

Greetings and thank you very much for your time. It has been a valuable help.

1 Like