AS2 (Applicability Statement 2) is a widely-used protocol for secure and reliable data exchange over the Internet. It’s particularly popular in business-to-business (B2B) transactions where security, authentication, and non-repudiation (the inability to deny having sent a message) are crucial. Here’s a breakdown of its basics along with some TypeScript code demonstrating sending and receiving messages with acknowledgment receipts.
Basics of AS2:
- Message Encryption and Signing: AS2 messages are encrypted and signed using digital certificates, ensuring confidentiality and integrity during transmission.
- Synchronous and Asynchronous MDNs: AS2 supports both synchronous and asynchronous Message Disposition Notifications (MDNs). MDNs are receipts confirming the receipt, processing, and delivery of messages.
- HTTP/HTTPS Transport: AS2 messages are transmitted over HTTP or HTTPS, leveraging the existing web infrastructure for secure data exchange.
- EDI Integration: AS2 is commonly used for Electronic Data Interchange (EDI), facilitating the exchange of structured business documents between trading partners.
Sample TypeScript Code:
Here’s a simple TypeScript code using the as2-node
library for handling AS2 messages:
import * as AS2 from 'as2-node';
// Sender's AS2 configuration
const senderConfig: AS2.AS2Options = {
myId: 'AS2Sender',
myPrivateKey: 'path/to/sender/private.key',
myCert: 'path/to/sender/certificate.crt',
partners: [
{
id: 'AS2Receiver',
url: 'https://receiver.example.com/as2',
publicKey: 'path/to/receiver/certificate.crt'
}
]
};
// Receiver's AS2 configuration
const receiverConfig: AS2.AS2Options = {
myId: 'AS2Receiver',
myPrivateKey: 'path/to/receiver/private.key',
myCert: 'path/to/receiver/certificate.crt',
partners: [
{
id: 'AS2Sender',
url: 'https://sender.example.com/as2',
publicKey: 'path/to/sender/certificate.crt'
}
]
};
// Sending AS2 message
async function sendAS2Message() {
try {
const message: AS2.AS2Message = {
contentType: 'application/json',
content: Buffer.from(JSON.stringify({ subject: 'Invoice', body: 'Invoice data here...' })),
headers: { 'Message-Id': '1234567890' }
};
const response = await AS2.send(senderConfig, message);
console.log('MDN received:', response);
} catch (error) {
console.error('Error sending AS2 message:', error);
}
}
// Receiving AS2 message
AS2.listen(receiverConfig, async (msg) => {
try {
console.log('Received AS2 message:', msg);
// Process received message...
// Sending MDN (receipt)
const mdn = await AS2.sendMDN(receiverConfig, msg, 'processed');
console.log('MDN sent:', mdn);
} catch (error) {
console.error('Error processing AS2 message:', error);
}
});
// Trigger sending AS2 message
sendAS2Message();
In this code:
- We import the
as2-node
library and define configurations for both sender and receiver. - For the sender, we specify its ID, private key, certificate, and partner’s details (ID, URL, and public key).
- For the receiver, we do the same, but with reversed partner details.
- We define a function
sendAS2Message()
to send an AS2 message asynchronously. - We listen for incoming AS2 messages on the receiver’s side using
AS2.listen()
, where we process the received message and send an MDN (receipt) back. MDN stands for Message Disposition Notification. It’s a receipt or acknowledgment message sent by the recipient of an AS2 message to confirm its receipt, processing, and disposition. MDNs provide essential information about the status of the transmitted message, such as whether it was successfully received, processed, and delivered. They help ensure the reliability and integrity of data exchange in AS2 transactions, providing parties with confidence that their messages have been properly handled. - Finally, we trigger sending an AS2 message by calling the
sendAS2Message()
function.
Make sure to install the as2-node
library using npm (npm install as2-node
) before running this code.