# Migrate From Flask-JWT to Flask-JWT-Extended

Yes. An agent replaces Flask-JWT with Flask-JWT-Extended, rebuilds the login route it used to provide, keeps your API's responses stable, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-flask-jwt-to-flask-jwt-extended/flask-jwt/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Flask-JWT has had no release since 0.3.2 in 2015, and its metadata requires `PyJWT>=1.4.0,<1.5.0`, so a project that depends on it cannot raise PyJWT. Flask-JWT-Extended is the maintained library people move to, and it is a different design: Flask-JWT gave you a login endpoint and a token format, and Flask-JWT-Extended gives you functions and expects you to write the endpoint. The agent reads how much the app relied on the old defaults. In an API like udemy-course/flask-rest-demo, `requirements.txt` pins `Flask-JWT==0.3.2` beside `PyJWT==1.4.2`. `restdemo/__init__.py` builds `JWT(None, UserModel.authenticate, UserModel.identity)`, and that one line is the whole login feature. `config.py` sets `JWT_AUTH_URL_RULE` to `/auth/login`, a five minute `JWT_EXPIRATION_DELTA`, and a `JWT_AUTH_HEADER_PREFIX` that defaults to `FLASK`. Three resource methods use `@jwt_required()`, and one reads `current_identity.username`.

The agent first writes down the contract the tests already hold. `python -m unittest discover` is the project's test command, and `test_login.py` expects a `POST` to `/auth/login` to answer 200 with an `access_token`, and a wrong password to answer 401 with exactly `{"description": "Invalid credentials", "error": "Bad Request", "status_code": 401}`. Other tests send the token as `FLASK <token>`. None of that survives a library swap by default: the new library has no login route, answers errors as `{"msg": ...}`, and expects `Bearer`.

So the agent keeps the contract in code. It adds a view at `/auth/login` that calls the existing `UserModel.authenticate` and returns `create_access_token(...)` under the same key. It maps the settings: `JWT_EXPIRATION_DELTA` to `JWT_ACCESS_TOKEN_EXPIRES`, since the new default is fifteen minutes, not five, and `JWT_AUTH_HEADER_PREFIX` to `JWT_HEADER_TYPE`. `current_identity` becomes `current_user`, backed by a `user_lookup_loader` that does what `UserModel.identity` did. The 401 body for bad credentials is written out in the new view, and the agent asks whether the other error bodies should keep the old three-key shape through the library's error loaders or move to `msg`.

One change is easy to miss and the agent tests for it. Flask-JWT stored the user id in an `identity` claim as an integer. Flask-JWT-Extended stores it in `sub`, and current PyJWT rejects a subject that is not a string: a token created with `identity=1` is refused with 422 and `Subject must be a string`. The agent passes `str(user.id)` and converts back in the loader. It also runs the suite with warnings shown, where PyJWT reports that the default `SECRET_KEY` in `config.py` is shorter than the 32 bytes it recommends for HS256, and lists that for the reviewer without changing the secret.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Migrate this Flask project from Flask-JWT to Flask-JWT-Extended. Before changing code, write down the current authentication contract from the code, config and tests: the login URL and its request and response bodies, the Authorization header prefix, token lifetime, the claims in the token, how views read the current user, and the status code and JSON body of each error (bad credentials, missing header, expired token, malformed token). Then replace the JWT(app, authenticate, identity) object with JWTManager, and write a login view at the same URL that uses the existing authenticate function and returns the token under the same key. Map every JWT_ setting to its new name and keep the values, including lifetime and header prefix, so that nothing changes just because a default is different. Replace current_identity with current_user and a user_lookup_loader, or with get_jwt_identity where only the id is needed. Pass the identity as a string and convert it where the user is loaded. For each error case, keep the old status code and body unless the request says otherwise, using the library's loader callbacks, and list any case where that was not possible. Update the manifests: remove Flask-JWT, add Flask-JWT-Extended, and drop or raise the PyJWT pin. Run the existing tests, add tests for the error cases the suite does not cover, and run with warnings enabled and report security-related warnings such as a short signing key without changing secrets. State plainly that tokens issued before this change will be rejected after it, and what a client sees when that happens. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes the app factory, the config, the resources that read the current user, the requirements file, and adds one login view. The description begins with the contract table: each part of the old behaviour, and beside it either unchanged or what it is now. For flask-rest-demo the login URL, the response key, the 401 body for bad credentials, the header prefix and the five minute lifetime are all listed as kept.

Below that is the deploy note, which is the part an operator needs. Tokens signed before the change carry an `identity` claim and none of the claims the new library requires, so they stop working at deploy. With a five minute lifetime that means clients log in again once; with long-lived tokens it means planning the cutover, and the agent states the configured lifetime so the reader can judge.

A reviewer checks the new error-case tests against what real clients handle, decides the open question on error body shape, and acts separately on the signing key warning. The diff contains no secret values, only the names of the settings that hold them.

## Run it on every security alert on PyJWT

This job is often started by a dependency alert: a scanner flags the old PyJWT, and it cannot be raised because Flask-JWT holds it there. Create an automation with a `webhook` trigger in the Project that holds the repository, and save the prompt above. GitHub signs its own webhook deliveries and cannot send the `Authorization: Bearer` header the trigger requires, so have a small handler on your server receive the alert event, keep only alerts for the `pyjwt` package, and forward the payload to the trigger's URL with the trigger's secret. Have the handler send an `Idempotency-Key` header to the trigger's URL built from the alert number, so a redelivered alert starts nothing new.

The alert arrives after the saved prompt as event text. It tells the agent which advisory prompted the work, and the pull request can then say whether the upgrade resolves it.

## Why run it on Rush?

Authentication changes are judged by what clients see, and the only way to know that is to exercise each case: log in, call with a good token, with none, with an expired one, with the wrong prefix. The agent runs those against the app in its own sandbox and keeps the commands and responses in the session transcript. A reviewer reading the pull request can open the run and see the 401 body for a missing header instead of trusting a sentence about it.

The sandbox also means a fresh install with current PyJWT, which is where the string-subject rule and the key-length warning come from. An old virtualenv with PyJWT 1.4 still in it would pass the same tests and hide both. Starting clean is what makes the result match what production will install.

## Frequently asked questions

### Does Flask-JWT-Extended have an /auth endpoint like Flask-JWT?

No. You write the login view yourself: check the credentials, call `create_access_token(identity=...)`, and return it. This is a few lines, and it lets you keep the URL and response body your clients already use.

### What replaces current_identity?

`current_user`, once you register a function with `@jwt.user_lookup_loader` that loads the user from the token's subject. If a view only needs the id, `get_jwt_identity()` returns it with no database call.

### Will existing tokens keep working after the switch?

No. The two libraries put the user id in different claims and the new one requires claims the old one never set. Every client has to log in again after the deploy, so check the token lifetime first and schedule accordingly.

### Why do I get 422 Subject must be a string?

Current PyJWT validates that the `sub` claim is a string, and Flask-JWT-Extended stores the identity there. Pass `str(user.id)` to `create_access_token` and convert it back to an integer when you look the user up.

### Can I keep my custom header prefix?

Yes. Set `JWT_HEADER_TYPE` to the prefix you used in `JWT_AUTH_HEADER_PREFIX`. Without it the library expects `Bearer` and answers 401 to a request that sends anything else.

## More jobs like this: Upgrade dependencies weekly

- [Migrate From Flask-RESTPlus to Flask-RESTX](https://getrush.ai/run/migrate-flask-restplus-to-flask-restx/flask-restplus/)
- [Migrate From Flask-Script to the Flask CLI](https://getrush.ai/run/migrate-flask-script-to-flask-cli/flask-script/)
- [Migrate From garyburd/redigo to gomodule/redigo](https://getrush.ai/run/migrate-garyburd-redigo-to-gomodule-redigo/redigo/)
- [Migrate a Go Test Suite From Ginkgo v1 to Ginkgo v2](https://getrush.ai/run/migrate-ginkgo-v1-to-v2/ginkgo/)
- [Replace go-bindata With go:embed in a Go Web Server](https://getrush.ai/run/migrate-go-bindata-to-embed/go-bindata/)
- [Replace mitchellh/go-homedir With os.UserHomeDir in a Go CLI](https://getrush.ai/run/migrate-go-homedir-to-os-userhomedir/go-homedir/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
