Skip to content

Create Record

One of the operation of DATA-API is to create an entity.

In order to create a specific entity, a POST request to /api/:entity_name with json format payload enclosed in the body is sent.

The payload format will vary according to your Entity Definition which will define which format should be sent to each property and which fields are required.

// e.g.: customer.json [Entity Definition]
{
  // ...,
  "fields": [
    {
      "key": "email",
      "label": "email",
      "type": "TextField",
      "validateRules": {
        "required": false,
        "maxLength": 5
      }
    },
    {
      "key": "full_name",
      "label": "Full Name",
      "type": "TextField",
      "validateRules": {
        "required": false
      }
    },
    {
      "key": "dob",
      "label": "Date of Birth",
      "type": "DateField",
      "validateRules": {
        "required": false
      },
      "typeOptions": {
        "time": false
      }
    },
    {
      "key": "country_id",
      "label": "Country",
      "type": "SingleDropDown",
      "relationshipOptions": {
        "many": false,
        "ref": "country"
      },
      "typeOptions": {
        "label": "name"
      },
      "validateRules": {
        "required": false
      }
    },
    {
      "key": "verified",
      "label": "Vefified",
      "type": "Checkbox",
      "validateRules": {
        "required": false
      }
    }
  ]
  // ...
}

Using the entity above as an example, It is possible to create an customer entity as following

// e.g: NodeJS example
fetch('https://april9.stack9.co/api/customer', {
  method: 'POST',
  headers: {
    'Api-Key': '<my_secret_api_key>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: 'new_email@domain.com.au',
    full_name: 'John Doe',
    dob: '1993-04-02',
    country_id: 1,
    verified: true,
  }),
})
  .then(response => response.json())
  .then(data => console.log(data)); 

/** OUTPUT:
 * {
 *     "id": 4,
 *     "entity": {
 *        email: 'new_email@domain.com.au'
 *        full_name: 'John Doe'
 *        dob: '1993-04-02'
 *        country_id: 1
 *        verified: true
 *     }
**/ }
Status CodeOperationResponse
201Resource Created successfully{ ...entity_props}
422Validation Error{ error: { _original: {}, details: [] } }
404Resource not found