I want to extract keys for each Salesforce object (including custom) from a Salesforce environment via the simple_salesforce python library. That is, I want a pandas dataframe that looks as follows:

object key
Account 001
Contact 003
... ...

For that, I connect via sf = simple_salesforce Salesforce(). How can I extract the desired information from this instance? Thanks for any help!

  • It's available as metadata of the object (type). You can call the Metadata API or use Anon Apex by combining this Q&A with this Q&A.
    – identigral
    Apr 19, 2022 at 16:19
  • Seems interesting but I don't quite understand. How can I make the request to Apex via simple salesforce in my case?
    – Corram
    Apr 29, 2022 at 19:37

1 Answer 1


The following works to create the desired table:

import pandas as pd
import simple_salesforce

sobjects = simple_salesforce.Salesforce().describe()["sobjects"]
key_prefixes = [[sf_obj["name"], sf_obj["keyPrefix"]] for sf_obj in sobjects]
key_prefixes_df = pd.DataFrame(key_prefixes, columns=["object", "key"])
  • Nice and concise, upvoted.
    – identigral
    Apr 29, 2022 at 21:31
  • v helpful. was able to output to a text file by adding path ='keyprefixes.txt' with open(path, 'a') as f: df_string = key_prefixes_df.to_string(index=False) f.write(df_string)
    – gorav
    Jun 9 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .