AS2 EDI Gateway
Standalone AS2 Gateway for Signed, Encrypted X12 Exchange
Overview
Enterprise-grade Node.js AS2 gateway handling inbound decryption, signature verification and outbound signed X12 EDI transmission with MDN receipts, partner key management and audit-grade logging.
The problem
Supply-chain partners such as D&H Distributing enforce strict AS2 requirements before accepting automated orders and invoices: AES-256 payload encryption, SHA-256 signing, X.509 certificate validation and synchronous or asynchronous MDN receipts. Ad-hoc scripts cannot satisfy that, and a single misconfigured certificate silently breaks the trading relationship.
The solution
Built a standalone gateway that owns the full AS2 handshake. Inbound messages are decrypted, signature-verified, decompressed and persisted before acknowledgement; outbound EDI is signed, encrypted and tracked until the partner MDN arrives. Certificate lifecycle, partner profiles, RBAC and dead-letter handling live in the same service so failures are visible rather than buried in logs.
Project gallery
Technical architecture
Partner-facing AS2 endpoint, crypto layer, and durable message pipeline
AS2 Endpoint (POST /as2)
Handles inbound messages, content-type negotiation, partner lookup by AS2-From and AS2-To, and MDN generation.
Crypto & Certificate Layer
AES-256 decryption, SHA-256 signature verification and X.509/OpenPGP key handling built on node-forge, asn1js, pkijs and openpgp.
Message Store & Dead Letter Queue
Durable MongoDB persistence with Redis-backed retries so a partner outage never loses an invoice or an order.
Ops Console & Diagnostics
Partner readiness checks, certificate expiry warnings, RBAC with 2FA and audit logs for every inbound and outbound exchange.
Methodology
- Mapped the partner's AS2 profile: encryption algorithm, signing digest, MDN mode and certificate chain
- Implemented the inbound pipeline with verification before persistence so tampered payloads never enter the system
- Added outbound retry with idempotency keys to prevent duplicate purchase orders
- Built certificate expiry monitoring and partner readiness diagnostics before cutover
- Ran interoperability tests against reference AS2 implementations before production exchange
Results & impact
Key impact
- Enabled automated X12 order and invoice exchange with a major distributor
- Removed manual upload and download steps from the order cycle
- Made certificate and MDN failures visible before they broke a trading relationship
- Provided audit logs suitable for partner security review
Challenges & solutions
Certificate Chain Mismatches
Added a readiness diagnostic that validates the full partner chain, algorithms and MDN expectations before sending real traffic
Duplicate Transmissions
Tracked message IDs and MDN state so retries never re-send an accepted interchange
Silent Partner Rejections
Parsed negative MDNs into actionable errors with the partner's original text preserved
Key implementation
Inbound AS2 Verification
export async function receiveAs2(req, res) {
const partner = await partners.findByAs2Id(req.headers["as2-from"]);
if (!partner) return res.status(401).send(mdnError("unknown-partner"));
// Verify signature and decrypt before anything touches storage
const payload = await as2.decryptAndVerify({
body: req.body,
privateKey: partner.ourPrivateKey,
partnerCert: partner.theirCertificate,
});
const interchange = x12.parse(payload.toString("utf8"));
const message = await messages.create({
partnerId: partner.id,
messageId: req.headers["message-id"],
interchange,
});
await queue.publish("edi.inbound", { messageId: message.id });
return res.type("application/xml").send(mdn.sign({ messageId: message.id, partner }));
}