[SalesForce] Converting the Salesforce Pod Id to ServerUrl/Name

Given a Salesforce Org Id, how can I determine the name and serverUrl to use with APIs?*

For example, given the Org Id 00DE00001234ooT I can get the Pod/Server identifier from the first character after the keyPrefix. In this case it is E, which I know is NA9.

There are some more details about the Pod identifier in the question What are Salesforce ID's composed of?

Why might this be useful?
From the comments… If you have a Salesforce Username, Password and OrgId and want to establish a Salesforce Session it is useful to know if you are dealing with a Sandbox or Production/Dev org. You can use the Pod Id from the OrgId to determine if it is a Sandbox and then call the correct login URL (login.salesforce.com versus test.salesforce.com).

* this will not work correctly for orgs that were previously migrated to other instances as the OrgId will still reference the original pod.

Best Answer

I found one possible solution in the source code for Workbench.

They maintain a mapping structure that includes the pod identifier:

"valuesToLabels" => array(
            "login" => array("Login: Production/Developer",""),
            "test"  => array("Login: Sandbox (test)",""),
            "prerellogin.pre" => array("Login: Pre-Release", ""),
            "na0-api" => array("NA0 (ssl)","0"),
            "na1-api" => array("NA1","3"),
            "na2-api" => array("NA2","4"),
            "na3-api" => array("NA3","5"),
            "na4-api" => array("NA4","6"),
            "na5-api" => array("NA5","7"),
            "na6-api" => array("NA6","8"),
            "na7-api" => array("NA7","A"),
            "na8-api" => array("NA8","C"),
            "na9-api" => array("NA9","E"),
            "na10-api" => array("NA10","F"),
            "na11-api" => array("NA11","G"),
            "na12-api" => array("NA12","U"),
            "na14-api" => array("NA14","d"),
            "ap0-api" => array("AP0 (ap)","1"),
            "ap1-api" => array("AP1","9"),
            "eu0-api" => array("EU0 (emea)","2"),
            "eu1-api" => array("EU1","D"),
            "eu2-api" => array("EU2","b"),
            "tapp0-api" => array("Sandbox: CS0 (tapp0)","T"),
            "cs1-api" => array("Sandbox: CS1","S"),
            "cs2-api" => array("Sandbox: CS2","R"),
            "cs3-api" => array("Sandbox: CS3","Q"),
            "cs4-api" => array("Sandbox: CS4","P"),
            "cs5-api" => array("Sandbox: CS5","O"),
            "cs6-api" => array("Sandbox: CS6","N"),
            "cs7-api" => array("Sandbox: CS7","M"),
            "cs8-api" => array("Sandbox: CS8","L"),
            "cs9-api" => array("Sandbox: CS9","K"),
            "cs10-api" => array("Sandbox: CS10","J"),
            "cs11-api" => array("Sandbox: CS11","Z"),
            "cs12-api" => array("Sandbox: CS12","V"),
            "cs13-api" => array("Sandbox: CS13","W"),
            "cs14-api" => array("Sandbox: CS14","c"),
            "cs15-api" => array("Sandbox: CS15","e"),
            "cs16-api" => array("Sandbox: CS16","f"),
            "cs17-api" => array("Sandbox: CS17","g"),
            "prerelna1.pre" => array("Pre-Release: NA1","t")
        )