function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sean RockSean Rock 

How to create a lead using the rest api

Hello

I'm new to SalesForce and want to create a Lead using the rest api. I've been directed to the documentation however I cannot find the specific section for that. Can anyone help?

Thanks.
AnudeepAnudeep (Salesforce Developers) 
I recommend reviewing the example from the documentation to get started. You can try something like this to create a lead
 
https://yourinstance.salesforce.com/services/data/v48.0/sobjects/Lead

Body JSON:
 
{
    "body": {
    "Salutation": "Mr.",
    "FirstName": "H",
    "LastName": "Sam",
    "Company": "Samosto"
    }
}

Header:
 
Authorization: Bearer 00D0o0000015jPn!ARgAQPiIGhuYGUG_c0HDKNR0hxTX9zS82Fv1lIuqn4rapFJHPR422gLyi10rF8Auukb._hj9pj532DP7IajQV36lyKpUNEXdxvL

Content-Type: application/json

Sforce-Auto-Assign: TRUE

Let me know if it helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!
 
Sean RockSean Rock
Thanks, that has got me on the right path. I am getting the following now.
 
[
    {
        "message": "Session expired or invalid",
        "errorCode": "INVALID_SESSION_ID"
    }
]

I'll do a search to see if I can get get past that next issue.



 
Sean RockSean Rock
Eventually after getting past the invalid session id, and a few others after that, i was able to finally get an access token. However, the description above did not result in a lead being created so i've unmarked as Best Answer because it didn't work.

So, does anyone know how to create a Lead using the rest api?

Thanks.
Sean RockSean Rock
This post is related to this https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IUjKQAW. I gotten past the challenge of getting a session ID however I'm at a standstill.
Lincoln McFarlandLincoln McFarland
fwiw i think the API has changed to Account (https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm). this is working for me
 
// to run:
//     node create_new_account.js --config ./config_na150_scan_email_app.json
//
// links:
// https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm



const axios = require("axios");
const qs = require("qs");
const yargs = require("yargs");

const argv = yargs
    .command('create_account', 'test creating salesforce leads', {
        config: {
            description: 'config',
            alias: 'c',
            type: 'string',
        }
    })
    .help()
    .alias('help', 'h')
    .argv;


let { salesforce, scanResultsURL } = require(argv.config);

const auth_data = qs.stringify({"username": salesforce.username,
				"password": salesforce.password + salesforce.security_token,
				"grant_type": "password",
				"client_id": salesforce.consumer_key,
				"client_secret": salesforce.customer_secret,
				"redirect_uri": salesforce.redirect_uri});

console.log("auth data", auth_data)

const auth_config = {
    method: "post",
    url: salesforce.oauth2_uri,
    data: auth_data,
    headers: {"Content-Type": "application/x-www-form-urlencoded"}
}


const action_url = "https://na<TBD>.salesforce.com/services/data/v51.0/sobjects/Account"

console.log('action url', action_url)

data = {
    "Name" : "test Salesforce account API"
}

async function sendResultsEmail() {

    // "get" token
    axios(auth_config).then(function (response) {

	auth_header = {"Authorization": "Bearer " + response["data"]["access_token"]}

	action_config = {
	    method: "post",
	    url: action_url,
	    headers: auth_header,
	    data: data
	}


	// use valid token to send email
	axios(action_config).then(function (response) {

	    console.log("action response", response["data"]); // TODO rm

	}).catch(function (error) {
	    console.log("authenticated passed, action failed")

	    console.log("action error", error); // TODO something
	})

    }).catch(function (error) {
	console.log("action token error", error); // TODO something
    })

}

 
James DornJames Dorn
Creating a Lead in Salesforce using the REST API involves making an HTTP POST request to Salesforce's REST endpoint for the Lead object. You would generally send a JSON payload containing the Lead fields you want to set. The endpoint usually looks like `https://your_instance.salesforce.com/services/data/vXX.0/sobjects/Lead/`.

If you're looking for a simpler, no-code solution, you can use Skyvia's cloud-based integration platform. Skyvia offers a pre-built Salesforce connector (https://skyvia.com/connectors/salesforce) that can interact with the Salesforce REST API behind the scenes. You can set up data integration without needing to write any API code, which can be particularly helpful if you're new to Salesforce. Simply configure the Skyvia Salesforce connector, map your fields, and you can create Leads either manually or on a schedule.