JavaScript SDK

Official JavaScript SDK for Node.js and browser environments.

Installation

npm install @truthmark/sdk

Node.js Example

const { TruthMarkClient } = require('@truthmark/sdk');

// Initialize
const client = new TruthMarkClient({
  baseUrl: 'http://localhost:8000'
});

// Encode
const result = await client.encode(
  './photo.jpg',
  'Copyright 2025 - My Company'
);

console.log(`Download: ${result.download_url}`);

// Decode
const decoded = await client.decode('./watermarked.jpg');
if (decoded.found) {
  console.log(`Message: ${decoded.message}`);
  console.log(`Confidence: ${decoded.confidence * 100}%`);
}

Browser/React Example

import { TruthMarkClient } from '@truthmark/sdk';

const client = new TruthMarkClient();

function WatermarkUploader() {
  const handleUpload = async (e) => {
    const file = e.target.files[0];
    
    // Encode image
    const result = await client.encode(
      file,
      'My watermark message'
    );
    
    // Open download link
    window.open(result.download_url, '_blank');
  };

  return (
    <input 
      type="file" 
      accept="image/*" 
      onChange={handleUpload} 
    />
  );
}

API Reference

new TruthMarkClient(config?)

Create a new client instance.

const client = new TruthMarkClient({
  baseUrl: 'https://api.truthmark.com',  // Optional
  apiKey: 'your-api-key'                 // Optional
});

encode(image, message)

Embed watermark into an image.

  • image: File path (Node.js) or File/Blob (browser)
  • message: Text to embed (max 500 chars)
// Returns Promise<EncodeResult>
{
  status: 'success',
  metadata: { psnr: 42.5, bits_embedded: 256 },
  download_url: 'https://...'
}

Advanced Examples

Next.js API Route

// app/api/watermark/route.ts
import { TruthMarkClient } from '@truthmark/sdk';

const client = new TruthMarkClient();

export async function POST(request: Request) {
  const formData = await request.formData();
  const file = formData.get('file') as File;
  const message = formData.get('message') as string;
  
  const result = await client.encode(file, message);
  return Response.json(result);
}

Express.js Middleware

const express = require('express');
const multer = require('multer');
const { TruthMarkClient } = require('@truthmark/sdk');

const app = express();
const upload = multer();
const client = new TruthMarkClient();

app.post('/watermark', upload.single('image'), async (req, res) => {
  const result = await client.encode(
    req.file.buffer,
    req.body.message
  );
  res.json(result);
});