Salesforce: Accessing Metadata API via Javascript

Recently, Salesforce has enabled the metadata API in Apex. However, this is only limited to page layout and custom metadata types at the moment. This is a good news for a developer like me as I need it to achieve this “natively” within Salesforce.

So what are the options that are available for the time being if we want to access the other metadata? One good way that I found in the community is JSforce. This free tool helps you to access the metadata API via javascript (and command line) with just a few easy steps.

Are you worrying about authentication? Don’t worry this tool supports oAuth authentication which secure your username/password from others.

Here is a quick example of using the tool to retrieve metadata for Account and Contact in a Visualforce Page. Note that as I’m using Visualforce Page, I can utilize the $Api.Session_Id to get my current session id within Salesforce instead 🙂


<apex:page docType="html-5.0" showHeader="false">
    <apex:includeScript value="" />

        var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}' });
        var fullNames = [ 'Account', 'Contact' ];'CustomObject', fullNames, function(err, res) {
            if (err) {
                return console.error(err);

            for (var i=0; i < res.length; i++) {
                var metadata = res[i];
                console.log("Full Name: " + metadata.fullName);