Adding questions to a form through a JSON post request

Hi,
So far, I have been able to create and deploy and new form through a JSON post request using python.

Here’s my code:-

import requests
import json

url = “KoboToolbox Form Building API

para = {“format”:“json”}

payload = {“name”: “postformtest”,
“asset_type”: “survey”}

headers = {‘Content-Type’: “application/json”,‘Accept’: “application/json”}

response = requests.post(url, data=json.dumps(payload), auth=(“myUsername”,“myPassword”), headers=headers, params=para)

print(“POST=”,response.status_code)

responseJSON=json.loads(response.text)

url2 = “https://kobo.humanitarianresponse.info/assets/“+responseJSON[‘uid’]+”/deployment/

payload2 = json.dumps({“active”: True})

response2 = requests.post(url2, data=payload2, auth=(“myUsername”,“myPassword”),
headers=headers, params=para)

I can’t find any documentation on how to structure my JSON to add questions to my form. Viewing my asset endpoints only shows the questions as “labels” and viewing it as ssjson seems give more details about the questions but I don’t understand how it can be used to add questions to my surveys or am I going about it the wrong way?

Any help would be greatly appreciated. :blush:

You can either include content alongside name and asset_type when you make your first POST or update it later with a PATCH. Try making a new project and adding a few questions using the form builder, and then visit the asset detail URL (https://kobo.humanitarianresponse.info/assets/<your-asset-uid>/?format=json). You should see your questions in the content object, which contains survey, choices, and settings—these should look familiar if you know XLSForm.

Alternatively, you could import XLSForm directly using the API as described in KoBo API examples, using new KPI endpoints.

3 Likes

Thanks. I have successfully added questions to my form using JSON. :grin:

1 Like

@sawan can you share the payload structure you had used to successfully add questions to the form using JSON?

2 Likes

@pankaj_prasad , Use this:

{
  "settings": {},
  "asset_type": "survey",
  "content": {
    "survey": [
      {
        "type": "decimal",
        "label": [
          "How tall is this building?"
        ],
        "required": false
      },
      {
        "type": "date",
        "label": [
          "When was this building built?"
        ],
        "required": false
      },
      {
        "type": "integer",
        "label": [
          "How many floors is the building?"
        ],
        "required": false
      },
      {
        "type": "select_one",
        "label": [
          "Select One Example"
        ],
        "required": false,
        "select_from_list_name": "qg5jd87"
      },
      {
        "type": "file",
        "label": [
          "Document"
        ],
        "required": false
      }
    ],
    "choices": [
      {
        "name": "option_1",
        "label": [
          "Option 1"
        ],
        "list_name": "qg5jd87"
      },
      {
        "name": "option_2",
        "label": [
          "Option 2"
        ],
        "list_name": "qg5jd87"
      }
    ]
  },
  "name": "Test Form"
}

Adding content block is the key.