Skip to content

Edit Record

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

In order to edit a specific entity, a PUT request to /api/:entity_name/:id 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 edit an customer entity as following

// e.g: NodeJS example
fetch('https://april9.stack9.co/api/customer/1', {
  method: 'PUT',
  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
204Resource edited successfully
422Validation Error{ error: { _original: {}, details: [] } }