How to use jsonwebtokens library in retool script?

  • Goal: I'm trying to import jsonwebtokens into retool so that I can use it to encode json data. When I try to run the code below, I get an error: "can't access lexical declaration 'registered_claims_schema' before initialization".

  • Steps:

  1. I've been referencing these posts from the forum: Importing js libraries - #3 by zewen and How do I use external libraries?

  2. I added the following libraries at the application level:
    https://cdnjs.cloudflare.com/ajax/libs/jsonwebtoken/9.0.2/index.js
    https://cdnjs.cloudflare.com/ajax/libs/jsonwebtoken/9.0.2/decode.min.js
    https://cdnjs.cloudflare.com/ajax/libs/jsonwebtoken/9.0.2/sign.min.js

  1. I used Object.keys(window) to verify that the new libraries were imported to the app. These are some of the values that show when I print to console: "jws", "validate", "validateOptions", "validatePayload".

  2. I've also tried using require to require the jsonwebtoken library, but 'require' was not defined.

// Secret key for signing the JWT
const secretKey = 'your-secret-key';

// Payload data to include in the JWT
const payload = jsonValue.value;

// Options for the JWT (e.g., expiration time)
const options = {
    expiresIn: '10h', // Token expires in 1 hour
};

// Create the token
var token = validatePayload(payload, secretKey, options)
console.log(token)

Thank you for your help!

Hi @asherman thanks for reaching out!

I'm wondering if you can move your logic to custom API authentication like this example in our docs: Custom API authentication | Retool Docs Would this work for you?

When trying to import these libraries in Retool apps, I'm seeing several errors like: This library's code doesn't seem to work in the browser. We only accept libraries packaged for the browser. As noted in our docs, custom libraries need to be packaged to work in the browser. You won't need require or import statements in JS queries for compatible libraries

1 Like