Generate images using signed URLs

Generating images using signed URLs is a bit more technical.

Here's an example using Node.js (JavaScript):

const crypto = require('crypto')

const templateId = 719

const BASE_URL = `https://previewify.app/generate/templates/${templateId}/signed`

const apiToken = '<YOUR_API_TOKEN>'

const fields = {
    'previewify:title': 'Hello from Node.js!',
    'previewify:image': 'https://previewify.app/assets/logo.svg',
}

// Encode fields
const base64Fields = Buffer.from(JSON.stringify(fields)).toString('base64')

// Create a signature using HMAC
const signature = crypto
    .createHmac('sha256', apiToken)
    .update(base64Fields)
    .digest('hex')

const imageUrl = `${BASE_URL}?fields=${base64Fields}&signature=${signature}`

console.log(imageUrl)

Or the same example in PHP:

<?php

$templateId = 719;

$baseUrl = "https://previewify.app/generate/templates/{$templateId}/signed";

$apiToken = '<YOUR_API_TOKEN>';

$fields = [
    'previewify:title' => 'Hello from PHP!',
    'previewify:image' => 'https://previewify.app/assets/logo.svg'
];

$base64Fields = base64_encode(json_encode($fields));

$signature = hash_hmac('sha256', $base64Fields, $apiToken);

echo "{$baseUrl}?fields={$base64Fields}&signature={$signature}";

What is happening?

  • We're encoding the fields using first: JSO and then BASE64
  • Creating a signature using HMAC which consumes the API encoded fields and API token
  • Build a URL with the fields and signature query strings