> For the complete documentation index, see [llms.txt](https://docs.fusion.vectra.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fusion.vectra.ai/api-recipes/recipes/create-a-jwt-request-token.md).

# Create a JWT request token

This simple recipe demonstrates how to encode a JWT request token and output it. The output can be used as the string to pass in the jwt params in the HTTP POST to /auth/token

{% stepper %}
{% step %}

### 🛠️ Enter Config Info

Fill out with values for the API key you already created.

{% tabs %}
{% tab title="Python" %}

```python
# 📢 Netography API 2023: 🔏 JWT Token Encoding
#
# Instructions:
# 1. Install the PyJWT package using pip: `pip install PyJWT`
# 2. Add your values to the config_json below.
# 3. Run the script to generate and print the encoded JWT token.

import jwt
import random
import time
import json

config_json = '''
{
  "APPNAME": "",       
  "APPKEY": "",          
  "SHORTNAME": "",       
  "SHARED_SECRET": ""   
}
'''
```

{% endtab %}

{% tab title="Node" %}

```javascript
// 📢 Netography API 2023: 🔏 JWT Token Encoding
//
// Instructions:
// 1. Install Node.js and npm (https://nodejs.org/)
// 2. Install the jsonwebtoken package using npm: `npm install jsonwebtoken`
// 3. Add your values to the configJson below.
// 4. Run the script to generate and print the encoded JWT token.

const jwt = require("jsonwebtoken");

const configJson = `
{
  "APPNAME": "",
  "APPKEY": "",
  "SHORTNAME": "",
  "SHARED_SECRET": ""
}
`;
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
# 📢 Netography API 2023: 🔏 JWT Token Encoding
#
# Instructions:
# 1. Install the jwt gem using: `gem install jwt`
# 2. Add your values to the config hash below.
# 3. Run the script to generate and print the encoded JWT token.

require 'jwt'
require 'time'

config = {
  'APPNAME' => '',
  'APPKEY' => '',
  'SHORTNAME' => '',
  'SHARED_SECRET' => ''
}
```

{% endtab %}

{% tab title="AWS Lambda (Node.js)" %}

```javascript
const jwt = require("jsonwebtoken");

exports.handler = async (event) => {
  const config = {
    APPNAME: "",
    APPKEY: "",
    SHORTNAME: "",
    SHARED_SECRET: "",
  };
```

{% endtab %}
{% endtabs %}
{% endstep %}

{% step %}

### 📦 Prepare JWT Payload

Set up the payload with necessary information for creating the JWT token.

{% tabs %}
{% tab title="Python" %}

```python
config = json.loads(config_json)

payload = {
    'iat': int(time.time()),
    'jti': random.randint(0,10000000),
    'appname': config['APPNAME'],
    'appkey': config['APPKEY'],
    'shortname': config['SHORTNAME']
}
```

{% endtab %}

{% tab title="Node" %}

```javascript
const config = JSON.parse(configJson);

const payload = {
  iat: Math.floor(Date.now() / 1000),
  jti: Math.floor(Math.random() * 10000000),
  appname: config.APPNAME,
  appkey: config.APPKEY,
  shortname: config.SHORTNAME
};
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
payload = {
  'iat' => Time.now.to_i,
  'jti' => rand(0..10000000),
  'appname' => config['APPNAME'],
  'appkey' => config['APPKEY'],
  'shortname' => config['SHORTNAME']
}
```

{% endtab %}

{% tab title="AWS Lambda (Node.js)" %}

```javascript
  const payload = {
    iat: Math.floor(Date.now() / 1000),
    jti: Math.floor(Math.random() * 10000000),
    appname: config.APPNAME,
    appkey: config.APPKEY,
    shortname: config.SHORTNAME,
  };
```

{% endtab %}
{% endtabs %}
{% endstep %}

{% step %}

### 🔏 Encode JWT Token and Print

Encode the payload with the shared secret using the PyJWT library, and print the encoded JWT token as a string to stdout.

{% tabs %}
{% tab title="Python" %}

```python
token = jwt.encode(payload, config['SHARED_SECRET'], algorithm="HS256")
print(token)
```

{% endtab %}

{% tab title="Node" %}

```javascript
const token = jwt.sign(payload, config.SHARED_SECRET, { algorithm: "HS256" });
console.log(token);
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
token = JWT.encode(payload, config['SHARED_SECRET'], 'HS256')
puts token
```

{% endtab %}

{% tab title="AWS Lambda (Node.js)" %}

```javascript
  const token = jwt.sign(payload, config.SHARED_SECRET, { algorithm: "HS256" });

  return {
    statusCode: 200,
    body: token,
  };
};
```

{% endtab %}
{% endtabs %}
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.fusion.vectra.ai/api-recipes/recipes/create-a-jwt-request-token.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
