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 Please refer to the documentation here:
https://{kf_url}/api/v2/assets/{asset_uid}/data/#bulk-updating-of-submissions
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
.
Thanks Josh! Iāll probe your script and give you feedback in short time. This will very usefull for my humanitarian work.
Greetings.
Hi @Josh, about your script:
- I donāt have a variable called āsubmission_idsā so I imagine you mean the variable ā_idā, is that correct?
- 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?
- 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.
- 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,
- Thatās correct
- If a question is outside of a group, you just reference it as
question_name
instead ofgroup_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. - Correct. You will need to use your account token (or whatever user token that has permission to edit submissions for a particular project).
- 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.
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.
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.
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.
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
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.
Hi Josh,
I tried your solution but the following request send me a {"payload":["Confirmation is required"]}
headers: AxiosHeaders {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: 'Token XXX',
},
baseURL: 'https://kobo.humanitarianresponse.info/api',
method: 'patch',
url: '/v2/assets/aQDZ2xhPUnNd43XzuQucVR/data/bulk/?format=json',
data: '{"payload":{"submissionId":["419488400"],"data":{"start":"2023-04-24T10:14:35.000Z"}}}',
Is this a limitation related to the start
property? I managed to get it working to update validation_status
.
Couldnāt find a solution by reading the API documentation and couldnāt find the related error message in Github.
Any help will be greatly appreciated.
Thanks.