Examples

These examples assume you have loaded the browser SDK or imported the ES module as DecodeBox.

Run one operation

executeOperation returns the resulting string. It rejects with a DecodeBoxError for invalid input, unknown operations, execution failures, or limits.

await DecodeBox.ready;
const encoded = await DecodeBox.executeOperation("to-base64", "Hello");
console.log(encoded); // SGVsbG8=

Run a recipe

A recipe runs steps in sequence. Check the returned error field before using the output. Input validation can also reject the call.

const result = await DecodeBox.bake("Hello", [
  { opId: "to-base64" },
  { opId: "from-base64" },
]);

if (result.error) throw new Error(result.error.message);
console.log(result.output, result.durationMs); // Hello, then the elapsed time

Detect a format

The detector returns candidates with a format, confidence, preview, and supporting evidence. An empty array means it found no candidates.

const candidates = await DecodeBox.detect("SGVsbG8sIHdvcmxkIQ==");
for (const { format, confidence, evidence } of candidates) {
  console.log(format, confidence, evidence);
}

Discover operation arguments

Use getOperation(id) to read the operation's argument names, types, and defaults. Use listOperations() to discover the full catalogue.

const operation = DecodeBox.getOperation("to-base64");
console.table(operation?.args ?? []);

When passing arguments to executeOperation, use the names from the operation metadata as keys. Use a string, number, or boolean for a scalar argument and pass { value, format } when specifying the format of a toggleString argument. Omitted arguments use the SDK defaults.