Skip to main content

Signed URL for Image Transformations

Signed URL with php

<?php
$api_key = '0c3c6d026858460abc4de1dcb4de15ac';
$string_to_sign = '0c3c6d026858460abc4de1dcb4de15ac/conversions?resize=300,300&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT';
$signature = urlencode(base64_encode(hash_hmac('sha1', $string_to_sign, $api_key, true)));
$signed_url = 'https://cdn.filespin.io/api/v1/assets/' . $string_to_sign . '&signature=' . $signature;
?>

Signed URL with python

import base64, hmac, hashlib, urllib.parse

API_KEY = '0c3c6d026858460abc4de1dcb4de15ac'
string_to_sign = '0c3c6d026858460abc4de1dcb4de15ac/conversions?resize=300,300&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT'
signature = urllib.parse.quote_plus(
base64.urlsafe_b64encode(
hmac.new(API_KEY.encode('utf-8'), string_to_sign.encode('utf-8'), hashlib.sha1).digest()
).decode('utf-8')
)
signed_url = 'https://cdn.filespin.io/api/v1/assets/%s&signature=%s' % (string_to_sign, signature)

Signed URL with java

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

String api_key = "0c3c6d026858460abc4de1dcb4de15ac";
String string_to_sign = "0c3c6d026858460abc4de1dcb4de15ac/conversions?resize=300,300&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT";
SecretKeySpec signingKey = new SecretKeySpec(api_key.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] hmac = mac.doFinal(string_to_sign.getBytes(StandardCharsets.UTF_8));
String signature = URLEncoder.encode(Base64.getEncoder().encodeToString(hmac), StandardCharsets.UTF_8);
String signed_url = "https://cdn.filespin.io/api/v1/assets/" + string_to_sign + "&signature=" + signature;

Signed URL with nodejs

const crypto = require("crypto");
const API_KEY = "0c3c6d026858460abc4de1dcb4de15ac";
const string_to_sign =
"f99255d2bf8142b29561641491e9940c/conversions?resize=500,500&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT";

const hash = crypto
.createHmac("sha1", API_KEY)
.update(string_to_sign)
.digest("base64");
const signature = encodeURIComponent(hash.replace(/\//g, "_"));

const signed_url = `https://cdn.filespin.io/api/v1/assets/${string_to_sign}&signature=${signature}`;
console.log(signed_url);

If Secure On-demand Images option is enabled in your settings, then you must use signed URLs for on-demand images.

info

URL signing code samples are under language tabs.

Steps to generate signed URL

Besides the File Id and size parameters, you'll need the below:-

ParameterTypeDescription
API KeystringYour API Key. This is available in your Authorization Settings page.
EXPIRYintegerSeconds since Unix Epoch time.
ACCESS_IDstringAccess Id that must be used to sign URLs. You can obtain your account's Access Id from the Authorization Settings page.

Signing a image URL is done by setting an expiry and signing the URL using your FileSpin API_KEY and Access Id. The signing process is as below:-

  1. Take the URL part staring with the ASSET_ID, excluding the signature part, like 0c3c6d026858460abc4de1dcb4de15ac/conversions?resize=300,300&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT, where expiry is seconds since Epoch time
  2. Using your API Key, Access Id and the above URL part as the string-to-sign, calculate the HMAC signature
  3. Encode the signature to a base64 string, then URL encode the resulting string and append it to the URL as signature query parameter value

Example signed image URL

https://cdn.filespin.io/api/v1/assets/0c3c6d026858460abc4de1dcb4de15ac/conversions?resize=300,300&expiry=1452894790&accessId=IZJTAMBQGAYDAMBQGAYDAMBQGAYDANKT&signature=vsR0_NFfeLEJPc8MXWMh2xI2Qvg%3D
warning

Note that the parameter values must be url-encoded otherwise FileSpin may return a authorization error.