Skip to content

fix(storage-uploadthing): set url field on uploaded media documents - #14250

Open
mbifulco wants to merge 4 commits into
payloadcms:mainfrom
craftworkco:fix/storage-uploadthing-missing-url
Open

mbifulco wants to merge 4 commits into
payloadcms:mainfrom
craftworkco:fix/storage-uploadthing-missing-url

Conversation

@mbifulco

@mbifulco mbifulco commented Oct 18, 2025 •

Copy link
Copy Markdown

What?

The UploadThing storage adapter had two critical bugs that prevented images from loading correctly:

  1. Missing URL field on upload: The handleUpload function only set _key and filename fields from the UploadThing API response, but not the url field. This caused newly uploaded images to have no URL in the database.

  2. Disabled generateURL hook: The disablePayloadAccessControl setting was commented out, which prevented Payload's cloud storage plugin from calling adapter.generateURL() in the afterRead hook. This caused the API to fall back to generating /api/media/file/* URLs instead of using UploadThing CDN URLs (utfs.io), making all images inaccessible.

How?

This PR includes two fixes, and a documentation change:

  1. Set URL on upload (handleUpload.ts): Add data.url = res.data?.url for both main file uploads and image size variant uploads, so newly uploaded files have the correct UploadThing CDN URL stored in the database.

  2. Enable disablePayloadAccessControl (index.ts): Uncomment the disablePayloadAccessControl: true setting when ACL is public-read. This ensures Payload calls adapter.generateURL() when serializing documents, which generates the correct UploadThing CDN URLs for existing files.

  3. Document JWT token requirement (README.md): Add documentation clarifying that this plugin requires UploadThing tokens in JWT format (starting with ey), not the sk_ secret key format. I spent a while trying to figure out why uploadThing wasn't working - their dashboard has 2 quick-copyable different formats for API key, and I was using the wrong one!

Testing

  • Verified new uploads correctly store UploadThing CDN URLs in database
  • Confirmed API responses return https://utfs.io/f/{key} URLs instead of /api/media/file/* URLs
  • Tested that images load correctly from the UploadThing CDN

Fixes: Images returning 400 errors and failing to load from /api/media/file/*

The handleUpload function was only setting _key and filename fields
from the UploadThing API response, but not the url field. This caused
uploaded images to fail loading because Payload couldn't find the
proper CDN URL.

This fix adds data.url = res.data?.url for both main file uploads
and image size variant uploads.

Fixes: Images returning 400 errors when trying to load from /api/media/file/
Closes: #[ISSUE_NUMBER]
@mbifulco
mbifulco requested a review from denolfe as a code owner October 18, 2025 01:47
…ment JWT token requirement

This commit adds two critical fixes:

1. Uncomment disablePayloadAccessControl for public-read ACL
   - When disabled/undefined, Payload cloud storage plugin does not call
     adapter.generateURL() in the afterRead hook
   - Without this, Payload falls back to /api/media/file/* URLs instead
     of using the UploadThing CDN URLs (utfs.io)
   - This makes images inaccessible since files are stored on UploadThing

2. Document JWT token format requirement in README
   - UploadThing plugin requires tokens in JWT format (starting with "ey")
   - Using sk_ format tokens will cause upload failures
   - Added clear documentation to prevent this common mistake
@mbifulco

Copy link
Copy Markdown
Author

Just wanted to note: we're using this change in production and it seems to be working great so far!

@mbifulco

mbifulco commented Nov 11, 2025 •

Copy link
Copy Markdown
Author

Hi team - anything I can do to help here? We have been using this change via pnpm patch without issue. If I can update the PR to help the process along, I'm happy to do so!

edit: created a little tutorial on how to use pnpm-patch in case it helps anyone who finds themselves here

@jhb-dev

jhb-dev commented Nov 11, 2025

Copy link
Copy Markdown
Contributor

Hi @mbifulco, point 1 (“Missing URL field on upload”) appears to be related to this issue I opened earlier today: #14562

After reviewing Payloads' source code, it looks like none of the official storage adapters set the url field within the getHandleUpload function, so this is not specific to the UploadThing adapter but rather a general pattern or issue across all adapters.

@mbifulco

Copy link
Copy Markdown
Author

Your PR looks great @jhb-dev - appreciate the video, that makes it super clear. Anything I can do to help with that issue at the moment?

@DanRibbens

Copy link
Copy Markdown
Contributor

Can anyone verify if this is still needed after the changes made in #15089?

@github-actions github-actions Bot removed the stale label Jan 18, 2026
@mbifulco

Copy link
Copy Markdown
Author

Hey @DanRibbens - I just tested with @payloadcms/[email protected] and can confirm this fix is still needed.

Looking at dist/handleUpload.js in 3.72.0, the URL assignment is still missing:

// Current code in 3.72.0 (lines ~25-32)
if (foundSize) {
    data.sizes[foundSize]._key = res.data?.key;
    // ❌ Missing: data.sizes[foundSize].url = res.data?.url;
} else {
    data._key = res.data?.key;
    data.filename = res.data?.name;
    // ❌ Missing: data.url = res.data?.url;
}

Result: Uploaded media URLs show http://localhost:3000/api/media/file/... instead of the UploadThing CDN URL (https://utfs.io/f/...). The _key is correctly stored, but url is not.

I'm still using the patch from this PR in production to work around it. Happy to provide any additional info that would help get this merged - or to hop on a call!

@github-actions github-actions Bot added the stale label Feb 24, 2026
@mbifulco

Copy link
Copy Markdown
Author

Anything else I can do to help here? Hate to see this go stale

@github-actions github-actions Bot removed the stale label Feb 25, 2026
@github-actions github-actions Bot added the stale label Mar 31, 2026
@mbifulco

Copy link
Copy Markdown
Author

This issue is still ongoing, insofar as I can tell.

@github-actions github-actions Bot removed the stale label Apr 1, 2026
@denolfe denolfe added the v3 label Apr 23, 2026
@github-actions github-actions Bot added the stale label May 24, 2026
@gb-developer

Copy link
Copy Markdown

Hey, finding similar issues and haven't been able to find a fix other than this.

@mbifulco

mbifulco commented Jul 7, 2026

Copy link
Copy Markdown
Author

Hey, finding similar issues and haven't been able to find a fix other than this.

FWIW my team is still using pnpm patch to work around this. LLMs have gotten very good at authoring patches, too, if you're uncomfortable with the process.

@github-actions github-actions Bot removed the stale label Jul 8, 2026
@github-actions github-actions Bot added the stale label Aug 8, 2026
@mbifulco

Copy link
Copy Markdown
Author

Anything else I can do to help here?

@github-actions github-actions Bot removed the stale label Aug 11, 2026
@github-actions github-actions Bot added the stale label Sep 11, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants