Template Functions
Use dynamic values from anywhere in Yaak
API requests within real-world applications usually require some sort of dynamic content, whether it’s hashing a value, reading a file, or prompting the user for input.
Template functions are Yaak’s way of enabling highly dynamic requests without writing code. Here’s how they work.
Referencing Template Functions
Template functions are similar to Yaak’s Environment Variables except they generate dynamic values instead of referencing static text.
Functions can be accessed via the autocomplete menu in any text input.
Yaak includes a number of built-in functions but it’s also easy to write your own by creating a Plugin with a few lines of TypeScript (see example below).
Custom Template Functions
Custom template functions can be added by creating and installing a plugin, with just a few lines of TypeScript. Here’s the entire source code for Yaak’s built-in fs plugin:
import * as fs from 'node:fs';
import { definePlugin } from '@yaakapp/api';
export default definePlugin({
templateFunctions: [{
name: 'fs.read',
args: [{ name: 'path', label: 'File Path' }],
async onRender(ctx, args) {
if (!args.values.path) return null;
try {
return fs.promises.readFile(args.values.path, 'utf-8');
} catch (err) {
return null;
}
},
}],
});
Check out the source of Yaak’s internal plugins for more sample code.
Was this page helpful?