|
| 1 | +/** |
| 2 | + * CommonJS-Only Nylas SDK Example |
| 3 | + * |
| 4 | + * This example demonstrates how to use the Nylas Node.js SDK in a pure CommonJS |
| 5 | + * (CJS) environment without ES module syntax. |
| 6 | + * |
| 7 | + * Purpose: |
| 8 | + * - Shows CommonJS require() syntax with the Nylas SDK |
| 9 | + * - Demonstrates environment variable handling in CommonJS |
| 10 | + * - Provides a simple messages listing example |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * 1. Copy the parent examples/.env.example to examples/.env |
| 14 | + * 2. Fill in your NYLAS_API_KEY and NYLAS_GRANT_ID in the .env file |
| 15 | + * 3. Run: node index.js (or npm start) |
| 16 | + * |
| 17 | + * Requirements: |
| 18 | + * - Node.js with CommonJS support (any Node.js version) |
| 19 | + * - Valid Nylas API credentials |
| 20 | + * - A grant with message access permissions |
| 21 | + */ |
| 22 | + |
| 23 | +const dotenv = require('dotenv'); |
| 24 | +const path = require('path'); |
| 25 | +const Nylas = require('nylas'); |
| 26 | +const { logger, maskSecret } = require('./utils/logger.js'); |
| 27 | + |
| 28 | +// Load from parent directory since this example lives in a subdirectory |
| 29 | +dotenv.config({ path: path.resolve(__dirname, '../.env') }); |
| 30 | + |
| 31 | +// Fail fast if credentials are missing to provide clear error messages |
| 32 | +const apiKey = process.env.NYLAS_API_KEY || ''; |
| 33 | +if (!apiKey) { |
| 34 | + throw new Error('NYLAS_API_KEY environment variable is not set'); |
| 35 | +} |
| 36 | + |
| 37 | +const grantId = process.env.NYLAS_GRANT_ID || ''; |
| 38 | +if (!grantId) { |
| 39 | + throw new Error('NYLAS_GRANT_ID environment variable is not set'); |
| 40 | +} |
| 41 | + |
| 42 | +// Initialize the Nylas client |
| 43 | +const nylas = new Nylas({ |
| 44 | + apiKey, |
| 45 | + apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com' |
| 46 | +}); |
| 47 | + |
| 48 | + |
| 49 | +/** |
| 50 | + * Main function to demonstrate basic Nylas SDK usage in CommonJS environment |
| 51 | + */ |
| 52 | +async function main() { |
| 53 | + try { |
| 54 | + logger.info('Listing messages...'); |
| 55 | + |
| 56 | + // Log runtime config for debugging without exposing sensitive data |
| 57 | + logger.debug('Runtime config', { |
| 58 | + apiKey: maskSecret(apiKey), |
| 59 | + grantId, |
| 60 | + apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com' |
| 61 | + }); |
| 62 | + |
| 63 | + // Use basic list operation to verify SDK functionality and connectivity |
| 64 | + const messages = await nylas.messages.list({ |
| 65 | + identifier: grantId, |
| 66 | + }); |
| 67 | + |
| 68 | + logger.success('Messages listed successfully'); |
| 69 | + |
| 70 | + // Extract only essential fields to avoid logging sensitive message content |
| 71 | + logger.info('Message subjects and ids', messages.data.map(m => ({ id: m.id, subject: m.subject }))); |
| 72 | + |
| 73 | + } catch (error) { |
| 74 | + logger.error('Failed to list messages'); |
| 75 | + logger.debug('Error details', error); |
| 76 | + // Exit with error code to indicate failure for automation/CI purposes |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +main(); |
0 commit comments