Skip to main content

Require a token on a web trigger

Make a web trigger reject every call that doesn't carry your secret token, so only the systems you trust can run it.

Applies to: Script Master for Jira and Script Master for Confluence Β· Cloud

A web trigger URL has no sign-in. Anyone who has the URL can call it, and the script runs as the app. The check below is the only thing that stops an unknown caller, so put it at the very start of the script.

Before you begin​

  • Jira or Confluence permission: Jira admin (the Administer Jira global permission) or Confluence admin.
  • App permission: an active Script Master license.
  • A long random token, for example one from a password generator. The calling system sends it with every call.

Add the token check​

  1. On the Web Triggers tab, open the web trigger.

  2. At the top of Backend JavaScript, add the check below and replace <your-access-token> with your token.

    const EXPECTED_TOKEN = 'Bearer <your-access-token>';

    if (request.headers['authorization']?.[0] !== EXPECTED_TOKEN) {
    console.warn('Call rejected: missing or wrong token');
    return { statusCode: 401, statusText: 'Unauthorized' };
    }

    // The rest of your script runs only for callers with the token
    return { statusCode: 200, statusText: 'OK' };
  3. Select Save.

  4. In the calling system, send the header Authorization: Bearer <your-access-token> with every call.

Result: Calls without the header, or with another token, get 401 and change nothing.

A rejected call counts as a successful call in History, so at most one every 5 minutes is kept. To keep every rejected call, log it with console.error instead: calls that log an error are always recorded, and show in red. See Run history.

PlaceholderDescription
<your-access-token>Your secret token. Use the same value in the script and in the caller.

Header names reach the script in lowercase, so read request.headers['authorization'], whatever case the caller uses.

Test the check​

  1. Under the editor, turn on Test this Web Trigger.
  2. Select Generate request example > GET request example, then select Test. Result shows 401.
  3. Add "authorization": ["Bearer <your-access-token>"] to headers in the request, then select Test again. Result shows 200.

Keep the token safe​

  • The token is part of the script. Everyone who can open Script Master on your site can read it, and so can anyone who sees an export of the web trigger.
  • Don't put the token in the URL. URLs end up in logs and browser history more often than headers do.
  • To change the token, update the script and the calling system together. Calls with the old token fail as soon as you save.