Documentation

How to translate EDI files

Article author
Admin

Translate EDI resources with the HTTP POST Method

In order to translate from EDI or translate to EDI use the following resources with the POST method:

  1. Read EDI file into JSON

    You can read two resources, X12 files and EDIFACT files/streams. To do so, execute HTTP POST to the resource URL, with the request body being the file to translate included as form-data content.

      1. URL: https://api.edination.com/v2/x12/read
      2. Header: Ocp-Apim-Subscription-Key: {API key}
      3. Request: X12 file as form-data
      4. Response: Array of X12Interchange
      1. URL: https://api.edination.com/v2/edifact/read
      2. Header: Ocp-Apim-Subscription-Key: {API key}
      3. Request: EDIFACT file as form-data
      4. Response: Array of EdifactInterchange

    The response will contain an array of EDI interchanges, encoded in application/json format.

    Example with Postman:

    translate-x12-to-json.png

  2. Write JSON to EDI file

    Again, you can write out two resources, X12 and EDIFACT. Execute HTTP POST to the resource URL, the request body must contain a single EDI interchange, included as application/json content.

      1. URL: https://api.edination.com/v2/x12/write
      2. Header: Ocp-Apim-Subscription-Key: {API key}
      3. Request: X12Interchange
      4. Response: X12 file as application/octet-stream
      1. URL: https://api.edination.com/v2/edifact/write
      2. Header: Ocp-Apim-Subscription-Key: {API key}
      3. Request: EdifactInterchange
      4. Response: EDIFACT file as application/octet-stream

    The response will contain an EDI interchange, encoded in application/octet-stream format.

    Example with Postman:

    translate-json-to-x12.png

Share this:

Was this article helpful?

Comments

1 comment

  • Comment author
    Gaurav

    Hi Team,

    We are unable to make a api call through nodeJS. When we are making a call we are getting 400 which suggest the edi file is not getting sent with the request. I have already shared the code snippet with you. We are able to replicate the issue on Postman as well when we dont pass the file as form-data.

    We also find it challenging to get a sampel example of this over google. So we have already raised a request (#140) on 3 May 2021 but could not hear from you on this so far.

    I am adding the code snippet we have tried and response is same for all there i.e 400 Status Code. Please advise.

    Sample Try: 1

    const fs = require("fs");
    const axios = require("axios");
    const FormData = require("form-data");

    let fileData = fs.readFileSync(__dirname + "/Sample.edi");
    uploadResource(fileData);

    function uploadResource(data) {
      const _resourceData = new FormData();
      _resourceData.append("name", "fileName");
      _resourceData.append("filename", data, { filename: "Sample.edi" });

      //console.log(_resourceData);
      // ,
      let _headers = Object.assign(_resourceData.getHeaders(), {
        "Ocp-Apim-Subscription-Key": "cf6680e979fc4fc2997aefad6a903082",
        "Content-Type": "multipart/form-data",
      });
      let endPoint =

      axios
        .post(endPoint, _resourceData, { headers: _headers })
        .then((data) => console.log("suucess"))
        .catch((err) => console.log(err.toJSON()));
    }

    Sample 2:
    const fs = require("fs");
    const axios = require("axios");
    const FormData = require("form-data");
    let fileContent;
    var newFile = fs.createReadStream(__dirname + "/sample.edi");
    // personally I'd function out the inner body here and just call
    // to the function and pass in the newFile
    newFile.on("data", function () {
      const form_data = new FormData();
      //newFile.pipe(fileContent);
      form_data.append("name", "fileName");
      form_data.append("fileName", newFile, { contentType: "application/edi-x12" });

      let _headers = Object.assign(form_data.getHeaders(), {
        "Ocp-Apim-Subscription-Key": "cf6680e979fc4fc2997aefad6a903082",
        //"Content-Type": "multipart/form-data",
      });
      console.log(form_data);
      const request_config = {
        method: "post",
        url:
        headers: _headers,
        data: form_data,
      };
      //console.log(request_config);

      axios(request_config)
        .then((d) => {
          console.log("success");
        })
        .catch((err) => console.log("err"));
    });

    Sample 3:
    const axios = require("axios");
    const FormData = require("form-data");
    const fs = require("fs");
    const form = new FormData();
    console.clear();

    let _headers = {
      "Ocp-Apim-Subscription-Key": "cf6680e979fc4fc2997aefad6a903082",
      //"Content-Type": "multipart/form-data",
    };

    console.log(_headers);

    const filePath = __dirname + "/edi.txt";
    let contentLength = form.getLengthSync();
    fs.readFile(filePath, (err, data) => {
      if (err) {
        throw err;
      }
      form.append("name", "fileName")
      form.append("fileName", data, {
        filepath: filePath,
        contentType: "application/octet-stream",
        "content-length": contentLength,
      });
      console.log(form);
      console.log("axios call started...");
      axios
        .post(
          form,
          {
            headers: _headers,
          }
        )
        .then((response) => {
          console.log(
            "success! ",
            response.status,
            response.statusText,
            response.headers,
            typeof response.data,
            Object.prototype.toString.apply(response.data)
          );
        })
        .catch((err) => {
          console.clear();
          console.log(err);
        });
    });
    Response: 
    FormData {
    _overheadLength: 273,
    _valueLength: 1289,
    _valuesToMeasure: [],
    writable: false,
    readable: true,
    dataSize: 0,
    maxDataSize: 2097152,
    pauseStreams: true,
    _released: false,
    _streams: [
    'Content-Disposition: form-data; name="name"\r\n' +
    '\r\n',
    'fileName',
    [Function: bound ],
    '----------------------------888347599097745748285135\r\n' +
    'Content-Disposition: form-data; name="filename"; filename="Sample.edi"\r\n' +
    'Content-Type: application/octet-stream\r\n' +
    '\r\n',
    <Buffer 49 53 41 2a 30 30 2a 20 20 20 20 20 20 20 20 20 20 2a 30 30 2a 20 20 20 20 20 20 20 20 20 20 2a 33 30 2a 38 30 39 37 30 31 31 39 36 20 20 20 20 20 20 ... 1231 more bytes>,
    [Function: bound ]
    ],
    _currentStream: null,
    _insideLoop: false,
    _pendingNext: false,
    _boundary: '--------------------------888347599097745748285135'
    }
    err.toJSON()
    PS D:\nodejs\ediNationAPIPOC> node .\test.js
    {
    message: 'Request failed with status code 400',
    name: 'Error',
    description: undefined,
    number: undefined,
    fileName: undefined,
    lineNumber: undefined,
    columnNumber: undefined,
    stack: 'Error: Request failed with status code 400\n' +
    ' at createError (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n' +
    ' at settle (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n' +
    ' at IncomingMessage.handleStreamEnd (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\adapters\\http.js:260:11)\n' +
    ' at IncomingMessage.emit (events.js:323:22)\n' +
    ' at endReadableNT (_stream_readable.js:1204:12)\n' +
    ' at processTicksAndRejections (internal/process/task_queues.js:84:21)',
    config: {
    url: 'https://api.edination.com/v2/x12/read?ignoreNullValues=false&continueOnError=false&charSet=utf-8',
    method: 'post',
    data: FormData {
    _overheadLength: 273,
    _valueLength: 1289,
    _valuesToMeasure: [],
    writable: false,
    readable: true,
    dataSize: 0,
    maxDataSize: 2097152,
    pauseStreams: true,
    _released: true,
    _streams: [],
    _currentStream: null,
    _insideLoop: false,
    _pendingNext: false,
    _boundary: '--------------------------776167331613242982930909',
    _events: [Object: null prototype],
    _eventsCount: 1
    headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'multipart/form-data; boundary=--------------------------776167331613242982930909',
    'Ocp-Apim-Subscription-Key': 'cf6680e979fc4fc2997aefad6a903082',
    'User-Agent': 'axios/0.21.1'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus]
    },
    code: undefined
    }
    PS D:\nodejs\ediNationAPIPOC> node .\test.js
    {
    message: 'Request failed with status code 400',
    name: 'Error',
    description: undefined,
    number: undefined,
    fileName: undefined,
    lineNumber: undefined,
    columnNumber: undefined,
    stack: 'Error: Request failed with status code 400\n' +
    ' at createError (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n' +
    ' at settle (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n' +
    ' at IncomingMessage.handleStreamEnd (D:\\nodejs\\ediNationAPIPOC\\node_modules\\axios\\lib\\adapters\\http.js:260:11)\n' +
    ' at IncomingMessage.emit (events.js:323:22)\n' +
    ' at endReadableNT (_stream_readable.js:1204:12)\n' +
    ' at processTicksAndRejections (internal/process/task_queues.js:84:21)',
    config: {
    url: 'https://api.edination.com/v2/x12/read?ignoreNullValues=false&continueOnError=false&charSet=utf-8',
    method: 'post',
    data: FormData {
    _overheadLength: 273,
    _valueLength: 1289,
    _valuesToMeasure: [],
    writable: false,
    readable: true,
    dataSize: 0,
    maxDataSize: 2097152,
    pauseStreams: true,
    _released: true,
    _streams: [],
    _currentStream: null,
    _insideLoop: false,
    _pendingNext: false,
    _boundary: '--------------------------092427793731219475939215',
    _events: [Object: null prototype],
    _eventsCount: 1
    },
    headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'multipart/form-data; boundary=--------------------------092427793731219475939215',
    'Ocp-Apim-Subscription-Key': 'cf6680e979fc4fc2997aefad6a903082',
    'User-Agent': 'axios/0.21.1'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus]
    },
    code: undefined
    }
    Sampel EDI File:
     
    =============
    ISA*00*          *00*          *30*809701196      *30*330897513      *210416*0950*^*00501*000000024*0*P*:~
    GS*HC*809701196*OA*20210416*0950*24001*X*005010X222A1~
    ST*837*1002*005010X222A1~
    BHT*0019*00*0000240075*20210416*0950*CH~
    NM1*41*2*Full Spectrum Chiropractic pllc*****46*809701196~
    PER*IC*Walker Yost*EM*dr.walkeryost@gmail.com*TE*3602693448~
    NM1*40*2*Sedgewick Management Services, INc*****46*330897513~
    HL*1**20*1~
    NM1*85*2*Full Spectrum Chiropractic pllc*****XX*1780085795~
    N3*101 Rogers  Street NW~
    N4*Olympia*WA*98502~
    REF*EI*103686947~
    HL*2*1*22*0~
    SBR*P*18*******AM~
    NM1*IL*1*Jenkins*Michael****MI*SZ92135~
    N3*8300 Carpenter Road Southeast*#25~
    N4*Lacey*WA*98503~
    DMG*D8*19540525*M~
    NM1*PR*2*Sedgewick Management Services, INc*****PI*PAPER~
    N3*PO Box 14447~
    N4*Lexington*KY*40512-4447~
    CLM*75*84***11:B:1*Y*A*Y*Y*P*EM~
    DTP*431*D8*20200802~
    CR2********D~
    HI*ABK:M9901*ABF:M9902*ABF:M9903*ABF:M9904*ABF:M54.2*ABF:M54.6*ABF:M54.5*ABF:M99.07*ABF:M25.512*ABF:M75.102*ABF:S39.012A~
    NM1*82*1*Yost*Walker****XX*1780085795~
    NM1*77*2*Full Spectrum Chiropractic pllc*****XX*1780085795~
    N3*101 Rogers  Street NW~
    N4*Olympia*WA*98502~
    LX*1~
    SV1*HC:2051A*84*UN*1***1:2:3:4~
    DTP*472*D8*20210227~
    REF*6R*751~
    SE*32*1002~
    GE*1*24001~
    IEA*1*000000024~
     
     
    ======

    This suggest that file data is being added as expected (check the length and form-data) but response (400 Status Code) does not suggest.

    Could you please advise as soon as possible. We are unable to proceed with the integration with testing it.

    Any working sample or code snippet will help. 




      

    0

Please sign in to leave a comment.