- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
The Nylas CLI (Command Line Interface) is a great way to interact with the Nylas APIs without the need of installing an SDK or calling the APIs via cURL. Why? Because everything is set up for us.
If we’re using a Mac or Linux based machine, we can use Homebrew. So let’s see how we can install the CLI using homebrew.
Feel free to skip this section if you have the CLI already installed on your computer.
If we don’t have Homebrew already installed, we can just copy and paste this on the terminal window:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is installed, we can simply type this command to install the Nylas CLI:
$ brew install nylas/nylas-cli/nylas
Or if we already have the Nylas CLI but want to make sure we have the latest version, we can do this to upgrade:
$ brew upgrade nylas
For Windows, WSL (Windows Subsystem for Linux) should be available in most Windows 11 machines, so we should be able to install both Brew and the Nylas CLI in the same way.
If we’re not using Windows 11 (or don’t prefer to use Homebrew), we can download these binary installers:
In the terminal, we can initialize the Nylas CLI with this command:
$ nylas init
Followed by our client ID and client secret. We can find this information on our Nylas dashboard.
Once that’s done, we can type:
$ nylas auth our_email_address
We will get the below message from Google, but that’s fine…we’re testing our app and not sending it to production.
We need to select everything, as we want our app to be able to access them all.
The CLI stores our access token for us.
As we can see, we’re verified and ready to go:
We can get some information about our account if we type
$ nylas api accounts get
The great thing about this is that we don’t need to authenticate ourselves again unless we either run nylas init
again or our access token needs to get revalidated.
The first thing we can do with the CLI is check our email. For that we can type
$ nylas api messages get --limit=2
If we don’t specify a limit, the call will fetch up to a hundred emails.
What if we don’t have any unread emails? Well, the Nylas API will fetch messages ordered by date with unread first followed by read emails as in the end, they all reside in the place and are only organized via labels.
To make sure we always read the inbox, we can use this
$ nylas api messages get --in=inbox
Also, there’s a nice trick we can use to get only the subjects of our emails
$ nylas api messages get --in=inbox --limit=3 | awk '/subject/{print}'
For sure, we’re not going to use the CLI as an email client…as reading emails like that can be a pain, but we can use it for more interesting things…
First, let create a JSON file called Email.json with the following content (replace it with your own information)
{ "subject": "Hello from Nylas CLI", "body": "Hello from Nylas CLI", "to": [{ "name": "Blag", "email": "[email protected]" }] }
Then back on the CLI, we need to go to the folder where we saved the file and type
$ cd Blag/Nylas_CLI $ nylas api send post < Email.json
If everything worked, you will see a message in the terminal with the details of the email and we can verify this by going into our receiving email account:
Of course, not everything in this world is Email, we also have Calendars and the CLI can help us too…
First, let’s create a JSON file to make things easier. We’re going to call it Calendar.json
{ "name": "A Nylas Calendar", "description": "Calendar coming from the CLI", "location": "Ottawa, Canada", "timezone": "America/Toronto" }
Then back on the CLI, we need to go to the folder where we saved the file and type
$ cd Blag/Nylas_CLI $ nylas api calendars post < Calendar.json
The new calendar has been created and we easily verify this by going into our Calendar and start checking…
Now that we have a new, fresh calendar, we can start by creating an event. Let’s create a file called Event.json
{ "title": "Party at Blag's House!", "location": "Blag's House", "when": { "start_time": 1643846400, "end_time": 1643932800 }, "participants": [{ "name": "Blag", "email": "<EMAIL_ADDRESS>" }], "calendar_id": "<CALENDAR_ID>" }
If you’re wondering about the weird numbers on “start_time” and “end_time”, please go to https://www.unixtimestamp.com.
$ nylas api events post < Event.json
We can always verify on the calendar that the event was created successfully…although keep in mind that this event happened in February.
We can create and modify contacts in an easy way. Let’s see how…we need to create a file called Contact.json
{ "given_name": "Alvaro", "surname": "Tejada Galindo", "nickname": "Blag", "birthday": "249022800", "company_name": "Nylas", "emails": [{ "type": "work", "email": "<EMAIL_ADDRESS>" }], "picture_url": "https://pbs.twimg.com/profile_images/1483394037902188545/I9vZIYxX_400x400.jpg" }
We can now create the contact
$ nylas api contacts post < Contact.json
In order to check our new contact, we can use
$ nylas api contacts get contact_id
Or we can check directly in our contacts
Now, let’s say we forgot something and we want to add it without having to delete and create the contact again, well…simply grab the contact id and do the following…we’re calling this file UpdateContact.json
{ "contact_id": "<CONTACT_ID>", "job_title": "Senior Developer Advocate", "office_location": "Ottawa" }
With the file ready, we can run this command from the console
$ nylas api contacts put <CONTACT_ID> < UpdateContact.json
We can verify that our changes were executed successfully.
Now, let’s say that we want to update an auto-generated contact…this happens when we get an email from someone who is not already on our contacts. The contact is created by default but it’s not really part of our address book. What we can do is, grab its ID and then download the information as a JSON file.
$ nylas api contacts get <CONTACT_ID> > NewContact.json
Once downloaded, we can update what we want.
{ "account_id": "<ACCOUNT_ID>", "birthday": null, "company_name": null, "emails": [ { "email": "[email protected]", "type": "personal" } ], "given_name": "Alvaro", "groups": [], "id": "<CONTACT_ID>", "im_addresses": [], "job_title": "Programming Languages Archaeologist", "manager_name": null, "middle_name": null, "nickname": null, "notes": null, "object": "contact", "office_location": null, "phone_numbers": [], "physical_addresses": [], "picture_url": null, "source": "inbox", "suffix": null, "surname": "Tejada Galindo", "web_pages": [ { "type": "homepage", "url": "https://blagarts.com" } ] }
Then we can type this on the console
$ nylas api contacts post < NewContact.json
Awesome! We have a new contact based on the auto-generated one.
I hope you liked working with the Nylas CLI. A powerful tool that can help us a lot of time. If you want to find out more, please visit the CLI Documentation.
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.