Need help in updating and deploying form using the API with Python

I am struggling with the below python code to update and deploy an existing form. I have looked at the examples here and here but still not figuring the issue.
I am a beginner in python but here is my code based on the above links:

import json
import requests
import pandas as pd

# converts Excel to json
def excel_to_json(excel_file_path):
        df = pd.read_excel(excel_file_path)  # Read the Excel file
        json_data = df.to_json(orient='records')  # Convert DataFrame to JSON
        return json_data
    except Exception as e:
        print("An error occurred during conversion:", str(e))

URL = ''

headers = {'Authorization': f'Token {TOKEN}'}
asset_url = "%sassets/%s/" % (URL, XFORM)

# get form as json from kobo
response = requests.get(asset_url, headers=headers, params={'format': 'json'})

# Convert XLS to JSON
json_data = excel_to_json(FILE_PATH)

response = requests.patch(asset_url, headers=headers, data={'content': json.dumps(json_data)})

# Deploying new version

response = requests.get(asset_url, headers=headers, params={'format': 'json'})
version_to_deploy = response.json()['version_id']
deployed_version_id = response.json()['deployed_version_id']

print("to deploy:" + version_to_deploy)
print("deployed: " + deployed_version_id)

response = requests.patch(asset_url + 'deployment/', headers=headers, data={'version_id': version_to_deploy})

Both print statements at the bottom show me the same version_id which means my PATCH did not work.


@Isslam, have you had a chance to look at this post discussed previously?

It could be helpful.

Thank you @Kal_Lam , I have looked at the previous discussion but still not working in my case. I only managed to make the “Replace form” works but not 100%. I can see the form was replaced when I go to the website but the updated form has 0 questions. I believe it’s an error when converting my xls to json. I’m not sure how to fix it. Also the Re-deployment request is giving me an error response of 500.

Here is an updated version of my code:

import json
import requests

URL = ''
TOKEN = 'token'
XFORM = 'form_id'
FILE_PATH = 'xls_path'

headers = {'Authorization': f'Token {TOKEN}'}
asset_url = "%sassets/%s/" % (URL, XFORM)

response = requests.get(asset_url, headers=headers, params={'format': 'json'})

data = open(FILE_PATH, 'rb')
files = {"file_name": data}

    payload = {'filename': FILE_PATH}
    files = {'content': open(FILE_PATH, 'rb')}
    data = {'description': 'Replace form', 'metadata': json.dumps(payload)}

    res = requests.patch(asset_url, headers=headers, data={'content': json.dumps(payload)})

response = requests.get(asset_url, headers=headers, params={'format': 'json'})
version_to_deploy = response.json()['version_id']
deployment_data = {
    'version_id': version_to_deploy,
    'active': True

headers = {
    'Accept': 'application/json',
    'Authorization': f'Token {TOKEN}'

response = requests.patch(asset_url + 'deployment/', headers=headers, data=deployment_data)
#response = requests.patch(asset_url + 'deployment/', headers=headers, data={'version_id': version_to_deploy})

This is what I’m using and I checked and they’re up to date. Thanks

Updating and deploying a form using an API with Python can vary greatly depending on the specific API and form service you are using. Below, I’ll provide a general guideline for working with APIs to update and deploy forms using Python:

Hi @chrisredfield . Thanks and I will be waiting for your response

Good Evening. I hope the information was helpful to you