> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtencrypt.md).

# JWTEncrypt

Encrypts a payload as a compact JWE (JSON Web Encryption) token. The payload can be a struct, a string (for nested JWT-in-JWE), or any value the module can serialize.

## Method Signature

```
JWTEncrypt( payload, [key], [options] )
```

### Arguments

| Argument  | Type     | Required | Description                                                          | Default |
| --------- | -------- | -------- | -------------------------------------------------------------------- | ------- |
| `payload` | `any`    | Yes      | Claims struct or value to encrypt.                                   |         |
| `key`     | `any`    | No       | Encryption key — named key, RSA public-key PEM, or symmetric secret. | `null`  |
| `options` | `struct` | No       | See options table below.                                             | `{}`    |

#### Options

| Option         | Type     | Description                                                           | Default        |
| -------------- | -------- | --------------------------------------------------------------------- | -------------- |
| `keyAlgorithm` | `string` | Key management algorithm: `RSA-OAEP-256` or `dir` (direct symmetric). | `RSA-OAEP-256` |
| `encAlgorithm` | `string` | Content encryption algorithm: `A256GCM`.                              | `A256GCM`      |
| `headers`      | `struct` | Custom JOSE headers (e.g. `kid`, `cty` for nested JWTs).              | `{}`           |

### Returns

A compact JWE string of the form `<header>.<encrypted-key>.<iv>.<ciphertext>.<tag>`.

## Examples

```javascript
// RSA key wrapping (asymmetric encryption)
token = jwtEncrypt( { sub: "u1", ssn: "123-45-6789" }, rsaPublicKeyPem, {
    keyAlgorithm: "RSA-OAEP-256",
    encAlgorithm: "A256GCM"
} );

// Direct symmetric encryption (32-byte key for A256GCM)
token = jwtEncrypt( { sub: "u1" }, secret32bytes, {
    keyAlgorithm: "dir",
    encAlgorithm: "A256GCM"
} );

// Nested JWT — sign first, then encrypt with cty: "JWT"
signed    = jwtCreate( { sub: "u1" }, signingKey, "RS256" );
encrypted = jwtEncrypt( signed, encryptionPubKey, {
    keyAlgorithm: "RSA-OAEP-256",
    encAlgorithm: "A256GCM",
    headers: { cty: "JWT" }
} );
```

## Related

* [JWTDecrypt()](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtdecrypt.md) — counterpart for decryption
* [Encryption (JWE) Guide](/boxlang-framework/modularity/jwt/encryption-jwe.md)
* [Algorithms](/boxlang-framework/modularity/jwt/reference/algorithms.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtencrypt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
