Trigger functions automatically with Stedi Core events and custom schedules
We want to showcase two features you can use to automatically invoke functions in your EDI integration: event bindings and function schedules.
Event bindings
You can configure event bindings on a function so the function is automatically invoked in response to specific Core events.
Example: Send inbound purchase orders to an ERP system
transaction.processed
events for inbound 850 Purchase Order transactions from a specific trading partner. When Core successfully processes an 850 transaction, it fires an event. The event binding invokes this function when the filter criteria are met.const buckets = bucketsClient();
const mappings = mappingsClient();
export const handler = async (event) => {
const transactionObject = await buckets.getObject({
bucketName: event.detail.output.bucketName,
key: event.detail.output.key,
});
const content = JSON.parse(await transactionObject.body?.transformToString());
const erpJson = await mappings.mapDocument({
id: "<your_mapping_id>",
content,
validationMode: "strict",
});
await fetch(process.env.ERP_CREATE_PURCHASE_ORDER, {
method: "POST",
body: JSON.stringify(erpJson),
headers: {
"Authorization": `Key ${process.env.ERP_API_KEY}`,
"Content-Type": "application/json",
},
});
};
Example: Publish processing failures to Slack
file.failed
events when it cannot process an inbound file or when it cannot deliver a generated outbound file to a partner. For example, Core emits a file.failed
event when your partner sends an invalid EDI file.file.failed
events and posts a Slack webhook to notify a support team to take action. You can use this approach to integrate with your own alerting system.export const handler = async (event) => {
const lines = [
`ISA IDs: receiver: ${event.interchange.receiverId},
sender: ${event.interchange.senderId}`,
`File ID: ${event.fileId}`,
`Direction: ${event.direction}`,
...event.errors.map((err) => `Error: ${err}`),
];
await fetch(process.env["SLACK_URL"], {
method: "POST",
body: JSON.stringify({
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "⚠️ Stedi Core File Processing Failed",
emoji: true,
},
},
...lines.map((line) => ({
type: "section",
text: {
type: "mrkdwn",
text: line,
},
})),
],
}),
});
};
Custom schedules
Example: Poll for inventory data
The following function polls to check inventory levels for product SKUs. When products run low on inventory, the function automatically generates an 850 purchase order based on current stock and preferred vendor information.
const mappings = mappingsClient();
const core = coreClient();
export const handler = async () => {
const lowInventoryItems = await fetch(process.env.ERP_LOW_INVENTORY_URL, {
method: "GET",
headers: {
Authorization: `Key ${process.env.ERP_API_KEY}`,
},
});
// Use Stedi mapping to transform JSON schema
const lowInventoryOrdersPromises = (await lowInventoryItems.json()).map(async (item) => {
const orderJson = await mappings.mapDocument({
id: "<your_mapping_id>",
content: item,
validationMode: "strict",
});
return core.generateEdi({
partnershipId: orderJson.partnershipId,
transactionGroups: [
{
transactionSettingsId: "004010-850",
transactions: orderJson.transactions,
},
],
});
});
await Promise.all(lowInventoryOrdersPromises);
};
Complete your integration with Stedi Functions
Compose end-to-end flows using any combination of Stedi products and external APIs. Create event-driven solutions that scale with your business.
Get blog posts delivered to your inbox.