Create data element API payload

I want to create data elements on fly. I have found an API to create new data elements , but I can’t find the payload to add in the body. Any help please

Could you please explain what you found exactly?

Thanks!

here is the link of what I found. They say that it is possible to create, read, update, delete, validate any metadata but they don’t provide sample payloads to be used.

Hi @Orly_MUGWANEZA,
The docs mention an example for the constants metadata; however, I will repeat the same example for the Data Elements specifically for your question

First, you need to get to the schema by going to ([server]/api/schemas?fields=href) and for Data Elements we will use the API endpoint ([server]/api/schemas/dataElement).
Second, now that you know the end point, you can figure out the required authorities (for the use to be able to make a successful request) as well as the required properties. For example, we can use this URL [server]/api/schemas/dataElement.json?fields=authorities,properties[fieldName,required]

I wasn’t able to filter out the not required but at least we can then use CTRL + F and search for ‘true’ to find out which ones are required.

The required fields are going to be:
“fieldName”: “aggregationType”
“fieldName”: “domainType”
“fieldName”: “categoryCombo”
“fieldName”: “valueType”
“fieldName”: “zeroIsSignificant”
“fieldName”: “name”
“fieldName”: “shortName”

So now you must include the above fields in the JSON file (or XML) payload, and you can generate an example from existing metadata. For example, here’s a like to get the JSON file format with the required fields: [server]/api/dataElements.json?fields=aggregationType,domainType,categoryCombo,valueType,zeroIsSignificant,name,shortName&filter=domainType:eq:TRACKER&pageSize=1

{
"name": "DataElementName",
"shortName": "DataElementShortName",
"aggregationType": "DataElementAggregationType",
"domainType": "TRACKER",
"valueType": "DataElementValueType",
"zeroIsSignificant": false or true?,
"categoryCombo": {
"id": "categoryComboID"
}
}

And if you are using XML then change the URL from .JSON to .XML

Finally, now that you have created and saved a .json file with all the required fields (and made sure that your user has the required authorities to perform the request), the payload would look like this:

curl -d **the file you created** "http://server/api/dataElements" -X POST
  -H "Content-Type: application/json" -u user:password

Hope this helps!:+1:

3 Likes

Thank you very much, this is what I was looking for.

1 Like

You’re welcome! :grin: