How to query an Installed Package Id

I'm not sure what sObject you can query (this has always been a bit troublesome), but there's an easy alternative: SFDX.

$ sfdx force:package:installed:list -u user-alias

ID                  Package ID          Package Name  Namespace        Package Version ID  Version Name     Version
──────────────────  ──────────────────  ────────────  ───────────────  ──────────────────  ───────────────  ────────
0A350000000R6wECAS  033A0000000TeSFIA0  Field Trip    Field_Trip       04tA0000000FTkoIAG  Ichiban          1.3.0.5

This gives you the 033 Id you need, as well as the 04t version ID, if you need that for other purposes.


For 1GP packages, I think that first Id never changes - looks to me as if it is actually the Id of the package in the packaging org (i.e. the Id in the URL when you got to create packages in the packaging org). I looked in a few customer/test orgs I have access to, and the Id is that same Id in each org.

So I am thinking you can just add the Id to a protected custom metadata setting, add to the package and query it when you need it. Combined with the PackageLicense info, you should have what you need.