Sdk integrationsJavascript
Quick Start
Get started with Cipherion in 5 minutes
Quick Start
This guide will help you encrypt and decrypt your first data in minutes.
Prerequisites
Before starting, ensure you have:
Step 1: Set Up Environment Variables
Create a .env file in your project root:
CIPHERION_BASE_URL=https://api.cipherion.in
CIPHERION_PROJECT_ID=your-project-id
CIPHERION_API_KEY=your-api-key
CIPHERION_PASSPHRASE=your-secure-passphraseYour passphrase must be at least 12 characters long
Step 2: Initialize the Client
import { CipherionClient } from '@cipherion/client';
const client = new CipherionClient({
baseUrl: process.env.CIPHERION_BASE_URL,
projectId: process.env.CIPHERION_PROJECT_ID,
apiKey: process.env.CIPHERION_API_KEY,
passphrase: process.env.CIPHERION_PASSPHRASE,
});Step 3: Encrypt Your First Data
Simple String Encryption
async function encryptString() {
const plaintext = "Hello, Cipherion!";
const encrypted = await client.encrypt(plaintext);
console.log('Encrypted:', encrypted);
// Output: "encrypted_token,mapped_data,iv"
}
encryptString();Complex Object Encryption
async function encryptObject() {
const userData = {
name: "John Doe",
email: "john@example.com",
phone: "+1234567890",
address: "123 Main St"
};
const result = await client.deepEncrypt(userData);
console.log('Encrypted data:', result.encrypted);
console.log('Total fields:', result.meta.totalFields);
console.log('Billable fields:', result.meta.billableFields);
}
encryptObject();Step 4: Decrypt Data
async function decryptData() {
// Decrypt simple string
const encrypted = "encrypted_token,mapped_data,iv";
const plaintext = await client.decrypt(encrypted);
console.log('Decrypted:', plaintext);
// Decrypt complex object
const encryptedObject = {
name: "encrypted_...",
email: "encrypted_...",
phone: "encrypted_...",
address: "encrypted_..."
};
const result = await client.deepDecrypt(encryptedObject);
console.log('Decrypted object:', result.data);
}
decryptData();Step 5: Handle Errors
import { CipherionError } from '@cipherion/client';
async function safeEncryption() {
try {
const encrypted = await client.encrypt("sensitive data");
console.log('Success:', encrypted);
} catch (error) {
if (error instanceof CipherionError) {
console.error('Encryption failed:', error.getUserMessage());
console.error('Status code:', error.statusCode);
} else {
console.error('Unexpected error:', error);
}
}
}
safeEncryption();Complete Example
import { CipherionClient, CipherionError } from '@cipherion/client';
import * as dotenv from 'dotenv';
dotenv.config();
const client = new CipherionClient();
async function main() {
try {
// Encrypt user data
const user = {
id: "user_123",
name: "Alice Smith",
email: "alice@example.com",
ssn: "123-45-6789"
};
console.log('Original user:', user);
// Encrypt, excluding the ID
const encrypted = await client.deepEncrypt(user, {
exclude_fields: ["id"]
});
console.log('Encrypted user:', encrypted.encrypted);
console.log('Cost: $', encrypted.meta.totalPrice);
// Decrypt back
const decrypted = await client.deepDecrypt(encrypted.encrypted, {
exclude_fields: ["id"]
});
console.log('Decrypted user:', decrypted.data);
} catch (error) {
if (error instanceof CipherionError) {
console.error('Error:', error.getUserMessage());
}
}
}
main();