Ghost Admin API - Invalid token: jwt expired

I had a working script and for some reason I now get:

/home/username/dev/myproject/myproject-backup/node_modules/@tryghost/admin-api/lib/admin-api.js:463
                const toThrow = new Error(props.message);
                                ^

Error [UnauthorizedError]: Invalid token: jwt expired
    at /home/username/dev/myproject/myproject-backup/node_modules/@tryghost/admin-api/lib/admin-api.js:463:33
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /home/username/dev/myproject/myproject-backup/fixUrls.js:103:15 {
  context: null,
  type: 'UnauthorizedError',
  details: null,
  property: null,
  help: null,
  code: 'INVALID_JWT',
  ghostErrorCode: null
}

My code:

const GhostAdminAPI = require("@tryghost/admin-api");

const BASE_URL = "myproject.com";

const api = new GhostAdminAPI({
  url: `https://${BASE_URL}`,
  version: "v5.0",
  key: "MyToken",
});

const POST_ID = "MyPostID";
const res = await api.posts.read({ id: POST_ID });

Version: 5.19.0 (docker on render)
I checked the server time and it is correct, also my local PC time.

It might be worth triple checking that what’s visible on the admin portal for your api key matches the key in this code, and the url matches, too.

Yes, I tried to recreate the key several times. Any other ideas?

I understand this may be a bit of a late reply to this post (1 year on), but I have just expierenced this exact issue.

My resolution, after about 2 hours of investigation, was to check the time on the server and client machine. The server time was 10 minutes out, and needed to be resynced.

2 Likes