Step 6 - Create the command-line script
Let's build a Node.js script that uses the Lightyear API to interact with HubSpot.
Script overview
Our script will:
- Connect to the Lightyear API
- Retrieve the HubSpot access token for our managed user
- Prompt for a company name
- Create the company in HubSpot
- Display the results
Set up the project
First, create a new directory for your script:
Write the script
Create a file called create-hubspot-company.js
:
Understanding the code
Let's break down the key parts:
Configuration
- We read the Lightyear API key from environment variables
- The managed user ID matches what we created earlier
- We default to Lightyear's production API URL
Getting the access token
This function retrieves the HubSpot access token that Lightyear is storing for our managed user.
Creating a company
This makes a direct API call to HubSpot using the retrieved access token.
Error handling
The script includes proper error handling for:
- Missing API keys
- Network failures
- API errors
- Invalid responses
What's next?
Great! You've written the script. Now let's run it and see it in action!