Home /  Core / 

Consuming events with Stedi Functions

After you configure Stedi Core, you can use the data flowing through Stedi to build end-to-end integrations with your business systems.
Core emits events as data arrives on Stedi. You can use Stedi functions to subscribe to these events and integrate Stedi with different systems and applications.
Stedi maintains a list of function templates that can help you set up common integration tasks. Templates come pre-configured with the necessary event bindings, and you can deploy them to your account.

Create a Stedi v2 function

Version Note: The following steps apply to the functions labeled New (v2) in the Functions UI.

To create a new function:

  1. Go to the Functions UI, and click Create function.
  2. Choose a Name and select a Bucket Name for the bucket where you want Stedi to store the function code.
It will take a few seconds for your new function to be provisioned within your account . The function's status changes from UNDER_CHANGE to AVAILABLE when it is ready.

Subscribe to events

To configure a function to subscribe to Core events:

  1. Click the function’s name to edit it.
  2. On the Overview tab, click the Add Event Binding button to open the binding editor.
  3. 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.
  4. Click Create.

Write function code

To edit a function's Javascript code, go to the Edit tab, edit the code, and then click Save. Stedi automatically deploys your changes.
The following example code accepts an incoming 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 };
};
While editing a simple function using the UI is quick and easy, if you need to include third-party dependencies from npm you will need to bundle and upload your code from your local machine or build process. For more information on deploying a Stedi Function via a CLI see our tutorial here.

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.

The following example pattern would match any file.processed event when type is “CSV” .
Example event pattern
{
  "detail-type": [ "file.processed" ],
  "detail": {
  	"source": {
    	"type": [ "CSV" ]
    }
  }
}
Example matching 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
  }
}
Create a Stedi v2 functionSubscribe to eventsWrite function codeCustom event patterns

Feedback

Have an idea for something we could improve? Page not clear? We love feedback - send us a message.

Stedi

Build EDI integrations fast, without being an EDI expert

Start building
About
ProductPricingCareersContactBlog
Follow
  1. Twitter
  2. GitHub
Backed by
AdditionBloomberg BetaFirst RoundStripeUSV
Customer AgreementService TermsPrivacy Notice

Stedi is a registered trademark of Stedi, Inc. All names, logos, and brands of third parties listed on our site are trademarks of their respective owners (including “X12”, which is a trademark of X12 Incorporated). Stedi, Inc. and its products and services are not endorsed by, sponsored by, or affiliated with these third parties. Our use of these names, logos, and brands is for identification purposes only, and does not imply any such endorsement, sponsorship, or affiliation.