Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Update data

POST data

Posts data to the API endpoint:

const apiName = 'MyApiName'; // replace this with your api name.
const path = '/path'; //replace this with the path you have configured on your API
const myInit = {
body: {}, // replace this with attributes you need
headers: {} // OPTIONAL
};
API.post(apiName, path, myInit)
.then((response) => {
// Add your code here
})
.catch((error) => {
console.log(error.response);
});

Example with async/await

async function postData() {
const apiName = 'MyApiName';
const path = '/path';
const myInit = {
body: {}, // replace this with attributes you need
headers: {} // OPTIONAL
};
return await API.post(apiName, path, myInit);
}
postData();

PUT data

When used together with a REST API, put() method can be used to create or update records. It updates the record if a matching record is found. Otherwise, a new record is created.

const apiName = 'MyApiName'; // replace this with your api name.
const path = '/path'; // replace this with the path you have configured on your API
const myInit = {
body: {}, // replace this with attributes you need
headers: {} // OPTIONAL
};
API.put(apiName, path, myInit)
.then((response) => {
// Add your code here
})
.catch((error) => {
console.log(error.response);
});

Example with async/await:

async function putData() {
const apiName = 'MyApiName';
const path = '/path';
const myInit = {
body: {}, // replace this with attributes you need
headers: {} // OPTIONAL
};
return await API.put(apiName, path, myInit);
}
putData();

Access body in the Lambda function

// using a basic lambda handler
exports.handler = (event, context) => {
console.log('body: ', event.body);
};
// using serverless express
app.put('/myendpoint', function (req, res) {
console.log('body: ', req.body);
});

Update a record:

const params = {
body: {
itemId: '12345',
itemDesc: ' update description'
}
};
const apiResponse = await API.put('MyTableCRUD', '/manage-items', params);

Access body in Lambda proxy function

// using a basic lambda handler
exports.handler = (event, context) => {
console.log('body: ', event.body);
};
// using serverless express
app.post('/myendpoint', function (req, res) {
console.log('body: ', req.body);
});