Home /  Guides / 

Generate EDI based on an implementation guide

Once you have a Stedi guide and a mapping based on that guide, you can start creating EDI documents automatically. You need some code that sends your original JSON document to Stedi Mappings and then sends the mapped document to EDI Translate. The result is a workflow that can translate JSON documents to valid X12 EDI.

Mapping JSON

The first step is to take a JSON document and map it to something resembling EDI. This is why you created a mapping based on the Stedi guide. You can call that mapping from code, providing it with the document you want to translate.

const axios = require("axios");

const apiKey = process.env.STEDI_API_KEY;

async function mapDocument(mappingId, document) {
  if (typeof document === "string") {
    document = JSON.parse(document);
  }

  const response = await axios({
    method: "post",
    url: `https://mappings.us.stedi.com/2021-06-01/mappings/${mappingId}/map`,
    data: document,
    headers: { Authorization: `Key ${apiKey}` },
  });

  return response.data;
}

Generating EDI

The output from Stedi Mappings is a JSON document that is structured according to the Stedi guide. It’s closer to EDI than the original document, but it’s still JSON. EDI Translate will translate it to an actual EDI document.

EDI Translate needs three things.

  • The source document. This is the JSON document produced by Mappings.
  • The guide ID. This help EDI Translate interpret the source document correctly.
  • Envelope information. Every EDI message has an envelope and you need to specify the data for it.

Envelopes

An EDI message has three envelopes.

  • The interchange tells you who the message is for on an organization level. It identifies you as the sender and your trading partner as the receiver.
  • The functional group tells you who the message is for on a department level. Invoices go to Finance department, ship notices go to the Shipping department, that sort of thing.
  • The transaction set represents a single document. An EDI message can contain multiple documents and the transaction set envelope helps keep them apart.

EDI Translate will take care of the transaction set envelope, but it needs your help with the interchange and the functional group.

Interchange fieldDescription
senderIdIdentifies the sender of the message.
senderQualifierIndicates the type of identifier used insenderId. For example, 01 means Duns, 20 means Health Industry Number, ZZ whatever you agree on. The implementation guide should tell you which values are valid. If not, contact your trading partner.
receiverIdIdentifies the receiver of the message.
receiverQualifierIndicates the type of identifier used inreceiverId. For example, 01 means Duns, 20 means Health Industry Number, ZZ whatever you agree on. The implementation guide should tell you which values are valid. If not, contact your trading partner.
dateThe date this message was sent, e.g.2022-08-15.
timeThe time this message was sent, e.g.13:20.
usageIndicatorCodeP if this is a production message, T if it’s a test message, I if the message is informational.
controlNumberA unique identifier for this interchange.
Functional group fieldDescription
functionalIdentifierCodeIndicates the type the message. Must be one of the standardized functional identifier code.
applicationSenderCodeIndicates where the message came from. There are no standardized codes; you need to agree on valid values with your trading partner. If you can’t find this in the implementation guide, contact your trading partner.
applicationReceiverCodeIndicates where the message should go. There are no standardized codes; you need to agree on valid values with your trading partner. If you can’t find this in the implementation guide, contact your trading partner.
dateThe date this message was sent, e.g.2022-08-15.
timeThe time this message was sent, e.g.13:20:52.
controlNumberA unique identifier for this functional group.
{
  "interchangeHeader": {
    "senderQualifier": "ZZ",
    "senderId": "ME",
    "receiverQualifier": "ZZ",
    "receiverId": "YOU",
    "date": "2022-08-15",
    "time": "13:20",
    "usageIndicatorCode": "P",
    "controlNumber": "12"
  },
  "groupHeader": {
    "functionalIdentifierCode": "PO",
    "applicationSenderCode": "ME",
    "applicationReceiverCode": "YOUR DEPT",
    "date": "2022-08-15",
    "time": "13:20:52",
    "controlNumber": "1"
  }
}

Calling EDI Translate

const axios = require("axios");

const apiKey = process.env.STEDI_API_KEY;

async function jsonToX12(
  guideId,
  document,
  // interchange envelope
  interchangeControlNumber,
  senderQualifier,
  senderId,
  receiverQualifier,
  receiverId,
  // functional group envelope
  functionalGroupControlNumber,
  functionalIdentifierCode,
  applicationSenderCode,
  applicationReceiverCode,
) {
  const now = new Date().toISOString();
  const dateNow = now.slice(0, 10);
  const timeNow = now.slice(11, 19);
  const timeNowShort = now.slice(11, 16);

  const response = await axios({
    method: "post",
    url: "https://edi-translate.us.stedi.com/2022-01-01/x12/from-json",
    data: {
      input: document,
      guideId: guideId,
      envelope: {
        interchangeHeader: {
          senderQualifier: senderQualifier,
          senderId: senderId,
          receiverQualifier: receiverQualifier,
          receiverId: receiverId,
          date: dateNow,
          time: timeNowShort,
          controlNumber: interchangeControlNumber,
          usageIndicatorCode: "P",
        },
        groupHeader: {
          functionalIdentifierCode: functionalIdentifierCode,
          applicationSenderCode: applicationSenderCode,
          applicationReceiverCode: applicationReceiverCode,
          date: dateNow,
          time: timeNow,
          controlNumber: functionalGroupControlNumber,
        },
      },
    },
    headers: { Authorization: `Key ${apiKey}` },
  });

  return response.data;
}
Mapping JSONGenerating EDI