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.

3 Likes

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

2 Likes

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.

2 Likes

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.

2 Likes

Thanks, @Josh for such a solution.
However, I experience something strange. After a successful update of given submission(s) and then later I tried to edit one of the submissions via the UI I got **kobo TypeError: Cannot read properties of null (reading 'parentElement')**. Any insight as to a way to solve this issue.
Our scenario involves several UI editing and then API editing and in the end some UI editing also of submissions.

Any insight would be appreciated

I believe the issue lies in the parent element as stated. After updating a submission via the API, the submission XML is missing **xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms"**. This might be the cause why enkento could not read load the submission in edit mode.

Hi @sheku_munu_kc, I apologize for the late response. There is a chance that this is related to this issue which has been fixed but not yet deployed into production (will be soon). Alternatively this can be related to some other things such as:

  • if your form has been updated with a version that’s now breaking previous submissions in Enketo;
  • Enketo has a bug
  • Something else

I would suggest, in the meantime, to continue using the bulk edit feature in the KoBo UI (the submission edit API) for these submissions.

1 Like

Thanks, Josh for your response.

Yes, it directly related to the issue
Our use-case will involve updating certain field(s) of submission via SMS, then the submission is processed further to other stages which require editing via the UI by other parties/individuals.

I know we are pulling strings to fit kobo solution into our use-case

Again thanks for your response and I hope the fix will be pushed soon.

2 Likes

Hi @Josh
Could you guide me how to edit repeat groups through a script?

Many thanks @Josh for your support
I’m trying to edit data using your code but always it shows 403 error. I’m using Pycharm to apply the code I don’t know if I’m missing something here.
PS: I used my secret token which i got it using this link :
https://kobo.humanitarianresponse.info/token/?format=json

Thanks in advance.

Hi @Ysr3322, a 403 status code indicates forbidden access. Please make sure you are setting the authentication headers correctly, that your token is correct and that you have edit permissions to perform this action.

Edit: I have just tested the same code snippet on my end, plugging in a new asset UID and token and it worked :+1:

2 Likes

Hi @ks_1, I apologize for missing this. Unfortunately we currently don’t support editing repeat groups yet, but it is still technically possible if you write your own script to grab the submission’s XML, make your edits, update the instanceID and deprecatedID values and then reupload to the server.

1 Like