Hi everyone!
I'm working with two separate Payload CMS applications. I need to fetch a collection from App B into App A using
@payloadcms/sdk.
Everything works, but I want to keep it type-safe. What is the best way to share or access the Config types from the second app?
yeah, the @payloadcms/sdk client is generic over the Config type - new PayloadSDK<Config>(...) - so all you need is App B's generated Config type available in App A. Don't hand-write it; reuse what payload generate:types already produces.
---
Monorepo (recommended): export App B's payload-types.ts and import it in App A.
import type { Config } from 'app-b/payload-types'
import { PayloadSDK } from '@payloadcms/sdk'
const sdk = new PayloadSDK<Config>({ baseURL: '
https://app-b.com/api' })
const { docs } = await sdk.find({ collection: 'posts' }) // fully typed to App B
Thanks for the answer! Since I'm NOT in a monorepo (App A and App B are completely separate repositories), what is the best practice to distribute that generated
payload-types.tsfrom App B to App A?
Oh that will be harder.
1. Publish a tiny types-only package
2. Git submodule / git-dependency (no registry needed)
Point App A's package.json at App B's repo directly:
"dependencies": {
"app-b-types": "github:yourorg/app-b#main"
}
App B keeps a committed payload-types.ts (it should anyway). Cheap, no publish pipeline - but you're pinned to a branch/commit, so it's coarser than semver.
I'll think about which one fits our workflow better. Appreciate the help
I can help on DM if needed!
Thank you very much, I'll text you if I need a help!
You can do it in a much simpler way as well. Just a script in package.json which copies the file from one project to another.
We have done that. Once you have new changes in CMS we generate types, and the types generation copies the generated types over to project B.
We always keep our schema backwards compatible, so this way using the old generated types will still work when the app's are deployed at different times.
This is one option too. The problem comes up when one person is on Windows and another is on a MacBook - but you could set up some kind of shared env to handle that. Yeah, this works as a solution as well.
But still cleanest way will be nx monorepo or trubo.
Thanks guys! Looking at all these monorepos, submodules, and CI/CD pipelines... I think I can easily survive without strict types for now. I only have 2 collections anyway, so typing them manually or using
anysounds like the best practice for my mental health right now 😂
Yeah, it can be a lot of infrastructure to set up for little gain. So you have to weigh it.
We use copy, monorepos and some where we create package. Each has their own pros and cons. Kacper mentioned some cons with one method.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.