Minimal example of creating a question using Qualtrics API | XM Community
Skip to main content

Hi all, I’m trying to use the Qualtrics API with Python in order to create a question in a survey following the API documentation https://api.qualtrics.com/5d41105e8d3b7-create-question, but I can’t seem to get even a minimal example working.

 

The code that is provided in the documentation is:

```

import requests
 
survey_id = "SV_8v0JD7wI8OFJNI2"
data_center_id = "fra1"
api_token = "OefAatpUHPQvVDWPVu0zmlpRnEq6XXp1LJkunaFr"
 
baseUrl = f"https://{data_center_id}.qualtrics.com/API/v3/survey-definitions/{survey_id}/blocks"
payload = {
  "ChoiceOrder": 4"1", "2"],
  "Choices": {
    "1": { "Display": "choice 1" },
    "2": { "Display": "choice 2" }
  },
  "Configuration": {
    "QuestionDescriptionOption": "UseText",
    "TextPosition": "inline",
    "ChoiceColumnWidth": 25,
    "RepeatHeaders": "none",
    "WhiteSpace": "ON",
    "LabelPosition": "BELOW",
    "NumColumns": 0,
    "MobileFirst": True
  },
  "DataExportTag": "string",
  "Language": u],
  "NextAnswerId": 0,
  "NextChoiceId": 0,
  "QuestionDescription": "string",
  "QuestionID": "string",
  "QuestionText": "string",
  "QuestionType": "MC",
  "Randomization": "string",
  "RecodeValues": {
    "1": "1",
    "2": "2",
    "3": "-1000"
  },
  "Selector": "DL",
  "SubSelector": "GR",
  "Validation": { "Settings": {
    "CustomValidation": {
      "Logic": {
        "Type": "BooleanExpression",
        "property1": {
          "ChoiceLocator": "string",
          "LogicType": "Question",
          "Operator": "Selected",
          "QuestionID": "string",
          "QuestionIsInLoop": "yes",
          "Type": "Expression"
        },
        "property2": {
          "ChoiceLocator": "string",
          "LogicType": "Question",
          "Operator": "Selected",
          "QuestionID": "string",
          "QuestionIsInLoop": "yes",
          "Type": "Expression"
        }
      },
    "Message": {
      "description": "string",
      "libraryID": "string",
      "messageID": "string",
      "subMessageID": "VE_FORCE_RESPONSE"
      }
  },
  "ForceResponse": "string",
  "ForceResponseType": "string",
  "Type": "string"
  } }
}
headers = {
  "Content-Type": "application/json",
  "Accept": "application/json",
  "X-API-TOKEN": api_token,
}
 
response = requests.post(baseUrl, json=payload, headers=headers)
 
response.json()
```
 
The error you get is quite long:
 
{'meta': {'requestId': '87304c3e-f182-43bb-aa98-09d82a4d6810', 'httpStatus': '400 - Bad Request', 'error': {'errorCode': 'QMST_1', 'errorMessage': 'The request was invalid.', 'code': 'ESDEF10', 'validationErrors': a{'Element': 'Blocks', 'Context': '(root)', 'Description': 'Must validate one and only one schema (oneOf)', 'Field': '(root)', 'Value': {'ChoiceOrder': e'1', '2'], 'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}}, 'Configuration': {'ChoiceColumnWidth': 25, 'LabelPosition': 'BELOW', 'MobileFirst': True, 'NumColumns': 0, 'QuestionDescriptionOption': 'UseText', 'RepeatHeaders': 'none', 'TextPosition': 'inline', 'WhiteSpace': 'ON'}, 'DataExportTag': 'string', 'Language': ], 'NextAnswerId': 0, 'NextChoiceId': 0, 'QuestionDescription': 'string', 'QuestionID': 'string', 'QuestionText': 'string', 'QuestionType': 'MC', 'Randomization': 'string', 'RecodeValues': {'1': '1', '2': '2', '3': '-1000'}, 'Selector': 'DL', 'SubSelector': 'GR', 'Validation': {'Settings': {'CustomValidation': {'Logic': {'Type': 'BooleanExpression', 'property1': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}, 'property2': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}}, 'Message': {'description': 'string', 'libraryID': 'string', 'messageID': 'string', 'subMessageID': 'VE_FORCE_RESPONSE'}}, 'ForceResponse': 'string', 'ForceResponseType': 'string', 'Type': 'string'}}}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Type is required', 'Field': 'Type', 'Value': {'ChoiceOrder': t'1', '2'], 'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}}, 'Configuration': {'ChoiceColumnWidth': 25, 'LabelPosition': 'BELOW', 'MobileFirst': True, 'NumColumns': 0, 'QuestionDescriptionOption': 'UseText', 'RepeatHeaders': 'none', 'TextPosition': 'inline', 'WhiteSpace': 'ON'}, 'DataExportTag': 'string', 'Language': ], 'NextAnswerId': 0, 'NextChoiceId': 0, 'QuestionDescription': 'string', 'QuestionID': 'string', 'QuestionText': 'string', 'QuestionType': 'MC', 'Randomization': 'string', 'RecodeValues': {'1': '1', '2': '2', '3': '-1000'}, 'Selector': 'DL', 'SubSelector': 'GR', 'Validation': {'Settings': {'CustomValidation': {'Logic': {'Type': 'BooleanExpression', 'property1': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}, 'property2': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}}, 'Message': {'description': 'string', 'libraryID': 'string', 'messageID': 'string', 'subMessageID': 'VE_FORCE_RESPONSE'}}, 'ForceResponse': 'string', 'ForceResponseType': 'string', 'Type': 'string'}}}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'SubType is required', 'Field': 'SubType', 'Value': {'ChoiceOrder': l'1', '2'], 'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}}, 'Configuration': {'ChoiceColumnWidth': 25, 'LabelPosition': 'BELOW', 'MobileFirst': True, 'NumColumns': 0, 'QuestionDescriptionOption': 'UseText', 'RepeatHeaders': 'none', 'TextPosition': 'inline', 'WhiteSpace': 'ON'}, 'DataExportTag': 'string', 'Language': ], 'NextAnswerId': 0, 'NextChoiceId': 0, 'QuestionDescription': 'string', 'QuestionID': 'string', 'QuestionText': 'string', 'QuestionType': 'MC', 'Randomization': 'string', 'RecodeValues': {'1': '1', '2': '2', '3': '-1000'}, 'Selector': 'DL', 'SubSelector': 'GR', 'Validation': {'Settings': {'CustomValidation': {'Logic': {'Type': 'BooleanExpression', 'property1': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}, 'property2': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}}, 'Message': {'description': 'string', 'libraryID': 'string', 'messageID': 'string', 'subMessageID': 'VE_FORCE_RESPONSE'}}, 'ForceResponse': 'string', 'ForceResponseType': 'string', 'Type': 'string'}}}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property DataExportTag is not allowed', 'Field': 'DataExportTag', 'Value': 'string'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property QuestionDescription is not allowed', 'Field': 'QuestionDescription', 'Value': 'string'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property SubSelector is not allowed', 'Field': 'SubSelector', 'Value': 'GR'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property NextChoiceId is not allowed', 'Field': 'NextChoiceId', 'Value': 0}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Randomization is not allowed', 'Field': 'Randomization', 'Value': 'string'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property ChoiceOrder is not allowed', 'Field': 'ChoiceOrder', 'Value': <'1', '2']}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property QuestionID is not allowed', 'Field': 'QuestionID', 'Value': 'string'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property QuestionType is not allowed', 'Field': 'QuestionType', 'Value': 'MC'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property RecodeValues is not allowed', 'Field': 'RecodeValues', 'Value': {'1': '1', '2': '2', '3': '-1000'}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Validation is not allowed', 'Field': 'Validation', 'Value': {'Settings': {'CustomValidation': {'Logic': {'Type': 'BooleanExpression', 'property1': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}, 'property2': {'ChoiceLocator': 'string', 'LogicType': 'Question', 'Operator': 'Selected', 'QuestionID': 'string', 'QuestionIsInLoop': 'yes', 'Type': 'Expression'}}, 'Message': {'description': 'string', 'libraryID': 'string', 'messageID': 'string', 'subMessageID': 'VE_FORCE_RESPONSE'}}, 'ForceResponse': 'string', 'ForceResponseType': 'string', 'Type': 'string'}}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Choices is not allowed', 'Field': 'Choices', 'Value': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Configuration is not allowed', 'Field': 'Configuration', 'Value': {'ChoiceColumnWidth': 25, 'LabelPosition': 'BELOW', 'MobileFirst': True, 'NumColumns': 0, 'QuestionDescriptionOption': 'UseText', 'RepeatHeaders': 'none', 'TextPosition': 'inline', 'WhiteSpace': 'ON'}}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Language is not allowed', 'Field': 'Language', 'Value': /]}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property NextAnswerId is not allowed', 'Field': 'NextAnswerId', 'Value': 0}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property QuestionText is not allowed', 'Field': 'QuestionText', 'Value': 'string'}, {'Element': 'Blocks', 'Context': '(root)', 'Description': 'Additional property Selector is not allowed', 'Field': 'Selector', 'Value': 'DL'}], 'message': 'Data validation failure'}}}
 
It’s not very helpful that there isn’t code that is provided to create a minimal example. Can anyone please help me create one?
 
I have tried to replace some of these “strings” with some values noted, but still don’t get anywhere.
 

Here’s a dummy code:

{
"QuestionType": "MC",
"QuestionText": "Something",
"Selector": "SAVR",
"SubSelector": "TX",
"ChoiceOrder": :"1", "2"],
"Choices": { "1": { "Display": "choice 1" }, "2": { "Display": "choice 2" } },
"Validation": { "Settings": { "ForceResponse": "OFF", "Type": "None" } },
"Configuration": {
"QuestionDescriptionOption": "UseText"
}
}

 


@ahmedA thanks for the response!

 

I have just tried that, full code:

import requests

survey_id = <survey-id>
data_center_id = "fra1"
api_token = <api-token>

baseUrl = f"https://{data_center_id}.qualtrics.com/API/v3/survey-definitions/{survey_id}/blocks"

payload = {
"QuestionType": "MC",
"QuestionText": "Something",
"Selector": "SAVR",
"SubSelector": "TX",
"ChoiceOrder": :"1", "2"],
"Choices": { "1": { "Display": "choice 1" }, "2": { "Display": "choice 2" } },
"Validation": { "Settings": { "ForceResponse": "OFF", "Type": "None" } },
"Configuration": {
"QuestionDescriptionOption": "UseText"
}
}

headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"X-API-TOKEN": api_token,
}

response = requests.post(baseUrl, json=payload, headers=headers)

response.json()

but this also returns an error:

{'meta': {'requestId': '75451722-b4d3-42ce-9396-af746f5c7db8',
'httpStatus': '400 - Bad Request',
'error': {'errorCode': 'QMST_1',
'errorMessage': 'The request was invalid.',
'code': 'ESDEF10',
'validationErrors': :{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Must validate one and only one schema (oneOf)',
'Field': '(root)',
'Value': {'ChoiceOrder': :'1', '2'],
'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}},
'Configuration': {'QuestionDescriptionOption': 'UseText'},
'QuestionText': 'Something',
'QuestionType': 'MC',
'Selector': 'SAVR',
'SubSelector': 'TX',
'Validation': {'Settings': {'ForceResponse': 'OFF', 'Type': 'None'}}}},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Type is required',
'Field': 'Type',
'Value': {'ChoiceOrder': :'1', '2'],
'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}},
'Configuration': {'QuestionDescriptionOption': 'UseText'},
'QuestionText': 'Something',
'QuestionType': 'MC',
'Selector': 'SAVR',
'SubSelector': 'TX',
'Validation': {'Settings': {'ForceResponse': 'OFF', 'Type': 'None'}}}},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'SubType is required',
'Field': 'SubType',
'Value': {'ChoiceOrder': :'1', '2'],
'Choices': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}},
'Configuration': {'QuestionDescriptionOption': 'UseText'},
'QuestionText': 'Something',
'QuestionType': 'MC',
'Selector': 'SAVR',
'SubSelector': 'TX',
'Validation': {'Settings': {'ForceResponse': 'OFF', 'Type': 'None'}}}},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property Validation is not allowed',
'Field': 'Validation',
'Value': {'Settings': {'ForceResponse': 'OFF', 'Type': 'None'}}},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property Configuration is not allowed',
'Field': 'Configuration',
'Value': {'QuestionDescriptionOption': 'UseText'}},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property QuestionType is not allowed',
'Field': 'QuestionType',
'Value': 'MC'},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property QuestionText is not allowed',
'Field': 'QuestionText',
'Value': 'Something'},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property Selector is not allowed',
'Field': 'Selector',
'Value': 'SAVR'},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property SubSelector is not allowed',
'Field': 'SubSelector',
'Value': 'TX'},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property ChoiceOrder is not allowed',
'Field': 'ChoiceOrder',
'Value': :'1', '2']},
{'Element': 'Blocks',
'Context': '(root)',
'Description': 'Additional property Choices is not allowed',
'Field': 'Choices',
'Value': {'1': {'Display': 'choice 1'}, '2': {'Display': 'choice 2'}}}],
'message': 'Data validation failure'}}}

 

any ideas on how to fix?


Leave a Reply