Snippets
What Snippets Are
Section titled “What Snippets Are”Snippets are reusable pieces of JavaScript you define once and pull into your check scripts with require(). Instead of pasting the same helper function, token-signing logic, or setup code into every check, you save it as a snippet and require it by name. Update it in one place and every check that uses it picks up the change.
A snippet is just a small JavaScript module. Whatever it exports with module.exports becomes available wherever you require it:
require('./snippets/name');Snippets are scoped to your team, so everyone on the team can require the same helpers from their checks.
Snippets work in setup scripts, teardown scripts, browser scripts and multistep scripts. When LoadFocus runs a check, it finds every require('./snippets/<name>') in your scripts and ships those snippets with the run. A snippet can use the same npm modules a script can (for example axios, got and crypto).
Manage Snippets
Section titled “Manage Snippets”Open the Snippets page to create, edit, and delete snippets.
Create a Snippet
Section titled “Create a Snippet”- Click New Snippet.
- Enter a Snippet Name. This is the name you'll require in your scripts (for example
authorformat-date). Names may contain letters, numbers, hyphens, and underscores only. - Write your JavaScript in the editor. Export whatever you want to reuse with
module.exports. - Click Save Snippet.
Each snippet name must be unique within your team and can be up to 64 characters long. The Free plan includes up to 5 snippets; paid plans include up to 100. A snippet can hold up to 100,000 characters.
Edit or Delete a Snippet
Section titled “Edit or Delete a Snippet”Use the row actions on the Snippets page to update a snippet's code or remove it. If a snippet is currently referenced by one or more checks, LoadFocus warns you before you save or delete it, because the change affects every check that requires it. Updating a snippet takes effect on the next run of any check that uses it.
Use a Snippet in a Check Script
Section titled “Use a Snippet in a Check Script”Require a snippet by name from the ./snippets/ path inside a check script:
const auth = require('./snippets/auth');The value you get back is whatever the snippet exports.
A setup or teardown script is already the body of a function, so write plain statements in it and do not add module.exports to the script itself.
Example
Section titled “Example”Create a snippet named auth that builds an Authorization header:
// snippet: authmodule.exports = function bearer(token) { return { Authorization: 'Bearer ' + token };};Then use it from a check script:
const bearer = require('./snippets/auth');
const headers = bearer(token);// `token` is a value your script already has, for example a secret// pass `headers` into your requestYou can keep any reusable logic in a snippet: request signing, payload builders, date and string helpers, or shared assertions, and require it from as many checks as you need.
Example: Log In From a Setup Script
Section titled “Example: Log In From a Setup Script”A snippet can also work with ctx. This snippet, named login, fetches a token and stores it in ctx.vars.token so the check can read it:
// Snippet: loginconst axios = require('axios');
module.exports = async function login(ctx, username, password) { const res = await axios.post('https://api.example.com/auth', { username, password }); ctx.vars.token = res.data.token;};Pass the credentials in as arguments: a ctx.secrets reference written only inside a snippet is not resolved for the run. In a browser check setup script:
const login = require('./snippets/login');
await login(ctx, 'ci-user', ctx.secrets.PASSWORD);// ctx.vars.token is now available to the checkAPI check scripts receive request (setup) or request and response (teardown) instead of ctx, so pass those to your snippet function.
Notes and Limitations
Section titled “Notes and Limitations”- Literal path only. Only the form
require('./snippets/<name>')is resolved. Dynamic paths such asrequire('./snippets/' + name)are not supported. - No snippet-to-snippet resolution. A snippet that requires another snippet is not resolved. Put shared logic into a single snippet instead.
- Missing snippets fail fast. If a required snippet does not exist, the check run fails before it is dispatched, with an error that names the missing snippet.
- Shared sandbox. Snippets run in the same sandbox as the calling script. A snippet only sees
ctx,requestorresponseif your script passes it in.
Snippets vs. Variables and Secrets
Section titled “Snippets vs. Variables and Secrets”Use Snippets for reusable code: helper functions and logic shared across check scripts.
Use Variables for reusable values such as base URLs and account ids, referenced with ${{VARIABLE_NAME}}.
Use Secrets for sensitive values such as API tokens and passwords. Secrets are write-only in the UI and referenced with ${{SECRET_NAME}}.
Tip: keep sensitive values in Secrets, not hard-coded in a snippet. Pass them into your snippet's functions at run time instead.