> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://support.pdfgeneratorapi.com/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to create JSON Web Token With Javascript?

The PDF Generator API uses **JSON Web Tokens (JWT)** to authenticate all API requests. These tokens offer a method to establish secure server-to-server authentication by transferring a compact JSON object with a signed payload of your account’s API Key and Secret. When authenticating to the PDF Generator API, a JWT should be generated uniquely by a server-side application and included as a Bearer Token in the header of each request.

### What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA. For more information about JSON Web Tokens check [jwt.io](https://jwt.io/).

### Creating JWT with Javascript

Here is an example code on how to create a JSON Web Token with Javascript. You can see the full example in CodePen [here](https://codepen.io/PDFGeneratorAPI/pen/JjeZEGe).

```javascript
import * as jose from 'jose'

createJsonWebToken(
    'the issuer',
    'the subject',
    'the secret'
  ).then((token) => {
     console.log(token)
  });

async function createJsonWebToken(iss, sub, secret) {

  const header = {
    alg: "HS256",// Token generation algorithm
    typ: "JWT"
  };

  const payload = {
    iss: iss,
    sub: sub,
    exp: Math.round(Date.now() / 1000) + 60 // token is valid for 60 seconds
  };

  return await new jose.SignJWT(payload)
    .setProtectedHeader(header)
    .sign(new TextEncoder().encode(secret));
}
```
---
### Related articles you may find helpful

* [Functions](https://support.pdfgeneratorapi.com/en/article/functions-srekaa/)
* [How to access a specific array element?](https://support.pdfgeneratorapi.com/en/article/how-to-access-a-specific-array-element-e3135c/)
* [Is it possible to conditionally hide a page in the document?](https://support.pdfgeneratorapi.com/en/article/is-it-possible-to-conditionally-hide-a-page-in-the-document-16d8p6b/)
