Consuming events with Stedi Functions
Create a Stedi v2 function
Version Note: The following steps apply to the functions labeledNew (v2)
in the Functions UI.
To create a new function:
- Go to the Functions UI, and click Create function.
- Choose a Name and select a Bucket Name for the bucket where you want Stedi to store the function code.
Subscribe to events
To configure a function to subscribe to Core events:
- Click the function’s name to edit it.
- On the Overview tab, click the Add Event Binding button to open the binding editor.
- In the binding editor, you can:
- Subscribe to one of the default Detail Types, like
transaction.processed
, by selecting it from the list. - Create a custom binding. A custom binding allows you to filter a specific subset of events using a custom event pattern. Visit Creating custom event patterns for details.
- Subscribe to one of the default Detail Types, like
- Click Create.
Write function code
transaction.processed
event, retrieves the translated JSON from the Core artifacts bucket, and delivers that translated JSON to a HTTP endpoint:const s3 = require("@aws-sdk/client-s3");
exports.handler = async function (event, context) {
// get bucket reference for the JSON version of EDI Transaction Set
const { output } = event;
// retrieve the file contents using bucket reference
const client = new s3.S3Client({});
const getFile = await client.send(
new s3.GetObjectCommand({ Bucket: output.bucketName, Key: output.key })
);
const raw = await getFile.Body.transformToString();
const ediAsJson = JSON.parse(raw);
// send JSON to endpoint
const result = await fetch("http://example.com/", {
method: "POST",
body: ediAsJSon,
});
return { ok: result.ok, statusCode: result.status, ediAsJson };
};
Custom event patterns
Event patterns are simple JSON objects that have the same structure as the events that they match.
You only need to include the fields in your event pattern that you want to match on, and the values must always be an array. This approach allows a single pattern to match many different events.
file.processed
event when type
is “CSV” .{
"detail-type": [ "file.processed" ],
"detail": {
"source": {
"type": [ "CSV" ]
}
}
}
file.processed
event for an incoming CSV{
"id": "xxx-05d4-b198-2fde-7ededc63b103",
"detail-type": "file.processed",
"source": "stedi.core",
"time": "2021-11-12T00:00:00Z",
"detail": {
"metadata": {
"processedAt": "2023-03-14T19:19:17.950Z"
},
"source": {
"type": "CSV",
"bucketName": "test-bucket",
"key": "123ABC/1746-1746-1.txt",
"size": 4301
}
}
Feedback
Have an idea for something we could improve? Page not clear? We love feedback - send us a message.