- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
We recently announced the launch of the Nylas API v3. In conjunction with updating the version of the API, we are releasing updated versions of the Nylas SDKs!
The latest version of the Nylas Node SDK not only improves functionality but also introduces major changes to align with modern JavaScript features such as ES6 imports. We also built out the Nylas Node SDK with Typescript support. This will support developers in creating and maintaining new integrations to the Nylas Calendar, Email and Contacts APIs.
In this blog post, we’ll delve into the benefits of building with the Nylas Node SDK using Typescript!
Check out our recent livestream covering this blog post:
Before diving into some code samples, let’s highlight what the Nylas Node SDK brings in the latest version:
TypeScript is an abstraction or superset of JavaScript. The benefit of building with types is the ability to support static typing which enables the following benefits:
Upgrading to a new version of an API can be daunting, especially with breaking changes. Let’s first ensure you have typescript installed and configured for your repository:
npm install typescript
If you do not have typescript configured for your project, consider running the following commands to set the recommended configuration:
npx tsc --init
Next, let’s grab the latest version of the Nylas Node SDK with Typescript support:
npm i nylas@latest
Ensure the version of the Nylas Node SDK is at least version 7.0.0. TypeScript can be your ally in this process:
1. Identifying Changes: Use TypeScript to detect areas in your code that need updates. Type errors will guide you through necessary modifications by running the watch command:
npx
tsc --watch
2. Refactoring with Confidence: TypeScript’s type system helps ensure that your refactoring doesn’t introduce new bugs. Here is an example of using the Nylas Node SDK types to access available functions to list messages:
TypeScript is a powerful tool when migrating between changes such as upgrading to a major version of the SDK.
A good example is migrating between the Nylas Node SDK (v6 to v7), where you can take advantage of the introduction of types to identify where breaking changes exist in your application:
In the above example, we are retrieving the first five calendars using the syntax for the Nylas Node SDK (v6) and have upgraded the SDK package to v7. We receive an error telling us that the argument is not correct and that we need to provide an identifier, which is the user’s grant identifier.
Here is the migration example from Nylas Node SDK v6 to v7:
// Nylas Node SDK v6:
const Nylas = require('nylas')
Nylas.config({
// replace with Nylas credentials
clientId: process.env.NYLAS_CLIENT_ID,
clientSecret: process.env.NYLAS_CLIENT_SECRET,
})
// Pass in connected user's access token
const nylas = Nylas.with(process.env.ACCESS_TOKEN)
const calendars = await nylas
.calendars
.list({limit: 5})
.then(calendars => console.log('Calendars:', calendars))
// Nylas Node SDK v7:
import 'dotenv/config'
import Nylas from 'nylas'
const NylasConfig = {
// replace with Nylas credentials
apiKey: process.env.NYLAS_API_KEY,
apiUri: process.env.NYLAS_API_URI,
}
const nylas = new Nylas(NylasConfig)
async function fetchFiveAvailableCalendars() {
const calendars = await nylas.calendars.list({
// Pass in connected user's grant id
identifier: process.env.NYLAS_GRANT_ID,
limit: 5
})
console.log('Calendars:', calendars)
}
fetchFiveAvailableCalendars()
To wrap up, we’ll share some best practices and tips for using TypeScript with the Nylas Node SDK:
The introduction of TypeScript support in Nylas API v3 is more than just a feature update; it’s a commitment to modern, robust, and efficient development. Start integrating TypeScript into your Nylas projects today and lead the way in innovative, type-safe application development.
You can sign up for Nylas for free and start building! Continue building with Nylas by exploring different quickstart guides or by visiting our developer documentation.
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible ????. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato ????.