> 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/fluent-builder.md).

# Fluent Builder

Compose JWTs fluently with jwtNew() — chain claim methods and terminate with .sign() or .encrypt().

`jwtNew()` returns a `JwtBuilder` — a chainable object that makes complex JWT construction read like an English sentence. Every claim or header method returns `this`, so calls flow naturally. Two terminal methods finish the chain:

* `.sign( [key], [algorithm] )` — produces a compact **JWS** string
* `.encrypt( [key], [keyAlgorithm], [encAlgorithm] )` — produces a compact **JWE** string

## Why Use the Builder?

| Functional BIF call                                                                | Builder equivalent                                                                      |
| ---------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| `jwtCreate( { sub:"u1", iss:"api", aud:"web", exp: now() + 3600 }, key, "HS256" )` | `jwtNew().subject("u1").issuer("api").audience("web").expireIn(3600).sign(key,"HS256")` |

The builder shines when you need many claims, custom headers, or want to assemble the payload conditionally.

## Anatomy of a Chain

```javascript
token = jwtNew()                          // 1. Start the chain
    .subject( "user-123" )                // 2. Standard claims
    .issuer( "my-api" )
    .audience( [ "web", "mobile" ] )
    .claim( "roles", [ "admin" ] )        // 3. Custom claim
    .expireIn( 3600 )                     // 4. Time claims
    .issuedNow()
    .jti( createUUID() )
    .header( "kid", "v2" )                // 5. JOSE headers
    .sign( secret, "HS256" );             // 6. Terminate → token string
```

## Conditional Composition

Because every method returns `this`, you can keep a reference to the builder and add claims based on logic:

```javascript
b = jwtNew().subject( user.id ).issuer( "my-api" ).expireIn( 3600 );

if ( user.isAdmin ) {
    b.claim( "scope", "admin:*" );
}
if ( user.tenant.isSet() ) {
    b.claim( "tenant", user.tenant );
}

token = b.sign( signingKey, "HS256" );
```

## Re-using an Existing Payload

Already have the claims as a struct? `.withPayload()` swaps in the whole struct at once, then you can layer additional methods on top:

```javascript
payload = { sub: "user-1", iss: "my-api", roles: [ "admin" ] };
token   = jwtNew()
    .withPayload( payload )
    .expireIn( 3600 )
    .sign( secret, "HS256" );
```

## Encrypting (JWE) Instead of Signing

Swap `.sign()` for `.encrypt()` — every other method works identically:

```javascript
token = jwtNew()
    .subject( "patient-456" )
    .claim( "phi", { dob: "1990-01-15" } )
    .expireIn( 900 )
    .encrypt( secret, "dir", "A256GCM" );
```

For asymmetric JWE just pass an RSA public key and `"RSA-OAEP-256"`:

```javascript
token = jwtNew()
    .subject( "patient-456" )
    .encrypt( publicKeyPem, "RSA-OAEP-256", "A256GCM" );
```

## Defaults Apply Here Too

If `defaultSigningKey` / `defaultAlgorithm` are set in module config, you can omit the terminal arguments entirely:

```javascript
token = jwtNew().subject( "u1" ).expireIn( 3600 ).sign();
```

## Reference Tables

The full method-by-method signatures live in the [JwtBuilder Fluent API Reference](/boxlang-framework/modularity/jwt/reference/fluent-api.md).

## Related

* [JWTNew()](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtnew.md)
* [JwtBuilder API Reference](/boxlang-framework/modularity/jwt/reference/fluent-api.md)
* [Signing (JWS)](/boxlang-framework/modularity/jwt/signing-jws.md)
* [Encryption (JWE)](/boxlang-framework/modularity/jwt/encryption-jwe.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 by asking a question.

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

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/fluent-builder.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.
