# Custom Code - Get firstname & lastname from email address

**URL:** <https://community.hubspot.com/t/custom-code-get-firstname-lastname-from-email-address/88815>\
**Category:** Data Hub\
**Created:** [September 1, 2023, 3:15pm UTC](https://community.hubspot.com/t/custom-code-get-firstname-lastname-from-email-address/88815 "2023-09-01T15:15:56Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Maciek-Magic](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Maciek-Magic](https://community.hubspot.com/u/Maciek-Magic)\
**Post date:** [September 12, 2023, 10:24am UTC](https://community.hubspot.com/t/custom-code-get-firstname-lastname-from-email-address/88815/5 "2023-09-12T10:24:04Z")

</div>

That should do the trick.

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {

 const hubspotClient = new hubspot.Client({
 accessToken: process.env.SECRET_NAME // Change "SECRET_NAME" with your Private App key secret name
 });

 let email;
 try {
 const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["email"]);
 email = ApiResponse.properties.email;

 let names = email.split("@"); // split emaill adress by "@"
 let fnln = names[0].split("."); // get whatever is before "@" and plit it by "."
 var fn = fnln[0]; //get whatever is before "." (First name)
 var ln = fnln[1]; // get whatever is after "." (Last name)
 if(fnln.length == 2 ){ // checking if First Name and Last Name exists together and are separeted by "."
 hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
 "properties": {
 "firstname": fn, // update contact record with First Name
 "lastname": ln // update contact record with Last Name
 }
 });
 } else{

 // WIll update First Name as John and Last Name as Doe
 hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
 "properties": {
 "firstname": "John",
 "lastname": "Doe"
 }
 });
 }
 } catch (err) {
 console.error(err);
 // We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
 throw err;
 }
}

```

---

_[View the full topic](https://community.hubspot.com/t/custom-code-get-firstname-lastname-from-email-address/88815)._
