This guide provides detailed instructions on how to send and receive data securely using encryption keys.
Encryption Overview
We prioritize the security of your data. To ensure this, we employ encryption for the data transmitted through our Public API and Webhooks.
Encryption Key
An encryption key is provided on the "Application" page of your account. This key is essential for encrypting and decrypting data exchanged between your application and our services.
Sending Data to Our Public API
You have the option to send data to our Public API either encrypted or unencrypted, depending on your preference and security requirements.
Encrypted Data: To send encrypted data, use the encryption key provided on the "Application" page. Encrypt your data before sending it to our API.The encrypted data should be included in the payload as follows:
{"data":"encryptedString"}
Unencrypted Data: If you prefer, you can also send data without encryption. This option is available for clients who may not require the added security layer.
Receiving Data from Public API and Webhooks
All data sent to you via our Public API or Webhooks is encrypted using the same encryption key found on the "Application" page. Ensure you decrypt the incoming data using this key to access the information.
Steps for Integration
Access Your Encryption Key: Log in to your account and navigate to the "Application" page to retrieve your encryption key.
Encrypt Your Data (optional): If you choose to encrypt your data, use the provided key to encrypt your JSON payloads.
Send Data to Public API:
Encrypted: Send your encrypted data to our Public API endpoint.
{"data":"encryptedString"}
Unencrypted: Send your plain JSON data to our Public API endpoint.
Receive and Decrypt Data: When receiving data from our Public Api or Webhooks, decrypt the incoming payload using the encryption key.
Example Code Snippets
Here are some example code snippets to help you get started with encryption and decryption using your key:
constcrypto=require('crypto');functiondecryptData(encryptedTextWithIv:string, key:string):string {constkeyBuff=Buffer.from(key,'hex');constiv=Buffer.from(encryptedTextWithIv.slice(0,32),'hex');constencryptedText=encryptedTextWithIv.slice(32);constdecipher=crypto.createDecipheriv('aes-256-cbc', keyBuff, iv);let decrypted =decipher.update(encryptedText,'hex','utf8'); decrypted +=decipher.final('utf8');return decrypted;}constaes256cbc=`*************`; // Your crypt key from application pageconstencryptedDataString='... Encrypted data in Base64 ...';constdecryptedDataString=decryptData(aes256cbc, encryptedDataString);console.log('Decrypted data object:',JSON.parse(decryptedDataString));
Decrypting Stream Files
In addition to handling encrypted data through our Public API and Webhooks, you may also need to decrypt stream files received from our services. Below are the steps and code snippets to help you decrypt these stream files effectively.
Steps for Decrypting Stream Files
Retrieve Initialization Vector (IV): The IV for the decryption process is provided in the response headers of the stream file. You can retrieve it using the following code:
Use the IV and the encryption key to decrypt the incoming stream file data.
The length of the IV is 16 bytes.
Example Code Snippet
Here's an example of how to decrypt a stream file:
constcrypto=require('crypto');constfs=require('fs');consthttp=require('http');const { promisify } =require('util');const { pipeline } =require('stream');constpipelineAsync=promisify(pipeline);asyncfunctiondownloadAndDecryptFile(url, outputPath, encryptionKey) {try {constresponse=awaitnewPromise((resolve, reject) => {constrequest=http.get(url, { headers: {'X-Private-App-Key':'string', }, }, (response) => {if (response.statusCode !==200) {reject(newError(`Failed to get '${url}' (${response.statusCode})`)); } else {resolve(response); } });request.on('error', (err) => {reject(err); }); });// Retrieve the IV from the headersconstiv=Buffer.from(response.headers['x-initialization-vector'],'hex');if (!iv ||iv.length!==16) {thrownewError('Invalid or missing initialization vector (IV)'); }constkeyBuff=Buffer.from(encryptionKey,'hex');// Create a decipher for decryptionconstdecipher=crypto.createDecipheriv('aes-256-cbc', keyBuff, iv);// Create a write stream for the output fileconstwriteStream=fs.createWriteStream(outputPath);// Pipe the encrypted stream through the decipher and into the write streamawaitpipelineAsync(response, decipher, writeStream);console.log('File downloaded and decrypted successfully.'); } catch (err) {console.error('Error downloading or decrypting file:', err); }}