> For the complete documentation index, see [llms.txt](https://xoops.gitbook.io/xmf-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xoops.gitbook.io/xmf-cookbook/reference/jwt/tokenfactory.md).

# TokenFactory

The `Xmf\Jwt\TokenFactory` class provides a convenient factory method to simplify creating JSON Web Token strings.

## TokenFactory::build(*$key*, *$payload*, *$expirationOffset*)

This method creates and returns a JSON Web Token string.

The *$key* is used to to sign the token. It may be specified as a [KeyAbstract](/xmf-cookbook/reference/key/keyabstract.md) object, or a key name string that will be used to build a key object using the [KeyFactory](/xmf-cookbook/reference/jwt/keyfactory.md) using a default [FileStorage](/xmf-cookbook/reference/key/filestorage.md) instance for storage.

The *$payload* is an associative array of claims included in the token, elements in the form claim => value.

The *$expirationOffset* is an integer that specifies an expiration time as the number of seconds from *now* that the token will expire. If specified, this will be added as an 'exp' claim to the payload.

If the token cannot be generated, an exception will be thrown. Possible exceptions include:

* DomainException;
* InvalidArgumentException;
* UnexpectedValueException;
