It's easy to call AWS API Gateway use SigV4
npm install fetch-sigv4 --save
OR
yarn add fetch-sigv4
endpoint
: required, your api gateway endpointmethod
: required, should be POST | GET | PUT | OPTIONSdata
: required for method POST | PUT, your object data to send to the apiaccessKeyId
: optional, default use AWS.config.credentialssecretAccessKey
: optional, default use AWS.config.credentialssessionToken
: optional, default use AWS.config.credentials
Configuration object:
var config = {
endpoint: "required --> your api gateway endpoint",
method: "POST | GET | PUT | OPTIONS",
data: [your object data to send to the api],
accessKeyId: "[your aws accessKeyId, default use AWS.config.credentials]",
secretAccessKey: "[your aws secretAccessKey, default use AWS.config.credentials]",
sessionToken: "[your aws sessionToken, default use AWS.config.credentials]"
}
var fetchApiSigv4 = require("fetch-sigv4")
var config = {
endpoint: "https://[your-api-id].execute-api.[region].amazonaws.com/[stage]/v1/authors",
method: "POST",
data: {
name: "Tam Nguyen",
country: "Viet Nam"
}
}
// var config = {
// endpoint: "https://[your-api-id].execute-api.[region].amazonaws.com/[stage]/v1/authors",
// method: "GET",
// headers: { "X-Request-ID": "1234" },
// secretAccessKey: "your secret access key",
// accessKeyId: "your access key id"
// }
const runDemo = async () => {
try {
console.log("Starting to run demo...");
const response = await fetchApiSigv4(config);
console.log("response data: ", response.data);
} catch (error) {
console.log("error: ", error);
}
}
runDemo();