Vonage Server SDK for Node.js

repository·main·Indexed 19 days ago

https://github.com/vonage/vonage-node-sdk

A programmatic interface for Node.js to access Vonage APIs, including SMS, Voice Calls, Text-to-Speech, Numbers, Verify (2FA), Video, Authentication, Conversations, Audit, and Identity Insights. The SDK is available as a comprehensive monorepo (@vonage/server-sdk) or as standalone packages such as @vonage/accounts, @vonage/applications, @vonage/audit, @vonage/auth, @vonage/conversations, and @vonage/identity-insights.

Tokens
91.3K
Snippets
356
Records
476
Agent score
63%

What's inside vonage-node-sdk

  1. Generate Asynchronous Reports with createReport

    main

    Use createReport for infrequent, large data queries (up to tens of millions of records). The report is generated in the background. Vonage recommends limiting queries to a maximum of 7 million records.

    Workflow:

    1. Call createReport to start the request.
    2. Use getReport(requestId) to poll the status.
    3. Once requestStatus is no longer PENDING or PROCESSING, use the links.downloadReport.href to download the file.
    4. Use cancelReport(requestId) if you need to stop a pending report.
    // Create the report
    const report = await reportsClient.createReport({
      product: 'SMS',
      accountId: API_KEY,
      dateStart: '2024-01-01T00:00:00Z',
      dateEnd: '2024-02-01T00:00:00Z',
      callbackUrl: 'https://example.com/webhook',
    });
    
    console.log(`Report ID: ${report.requestId}`);
    
    // Poll until complete
    let status = await reportsClient.getReport(report.requestId);
    
    while (status.requestStatus === 'PENDING' || status.requestStatus === 'PROCESSING') {
      await new Promise((resolve) => setTimeout(resolve, 5000));
      status = await reportsClient.getReport(report.requestId);
    }
    
    console.log(`Download: ${status.links.downloadReport?.href}`);
    
    // Cancel a pending report
    await reportsClient.cancelReport(report.requestId);
  2. Handle Verify V2 API responses with Promises

    main

    Most methods in the Verify V2 SDK return Promises. You can handle responses using .then()/.catch() or by using await within an async function.

    // Using await
    const resp = await verifyClient.checkCode(VERIFY_REQUEST_ID, CODE)
    
    // Using .then() and .catch()
    vonage.verify2
      .checkCode(VERIFY_REQUEST_ID, CODE)
      .then((resp) => console.log(resp))
      .catch((err) => console.error(err))
  3. Handle SMS API responses with Promises

    main

    Most methods in the SMS SDK return Promises. You can handle responses using standard .then()/.catch() chains or by using async/await syntax.

    // Using await
    const resp = await vonage.sms.basicLookup(PHONE_NUMBER);
    
    // Using .then()
    vonage.sms.getAvailableSMS()
      .then(resp => console.log(resp))
      .catch(err => console.error(err));
  4. Handle API responses with Promises or async/await

    main

    Most methods in the Number Insights SDK return Promises. You can handle responses using standard .then()/.catch() chains or by using await within an async function.

    // Using await
    const resp = await vonage.numberInsights.basicLookup(PHONE_NUMBER);
    
    // Using Promises
    vonage.numberInsights.basicLookup(PHONE_NUMBER)
      .then(resp => console.log(resp))
      .catch(err => console.error(err));
  5. Handle asynchronous Pricing API calls with Promises or async/await

    main

    Most methods in the Pricing SDK return Promises. You can handle responses using .then()/.catch() or by using the await keyword within an async function.

    // Using await
    const resp = await vonage.pricing.basicLookup(PHONE_NUMBER)
    
    // Using .then() and .catch()
    vonage.pricing.getAvailablePricing()
      .then(resp => console.log(resp))
      .catch(err => console.error(err));
  6. Handle Verify API responses with Promises

    main

    Most methods in the Verify SDK return Promises. You can handle responses using .then()/.catch() or by using await within an async function.

    // Using await
    const resp = await vonage.verify.check(VERIFY_REQUEST_ID, CODE)
    
    // Using .then() and .catch()
    vonage.verify
      .check(VERIFY_REQUEST_ID, CODE)
      .then((resp) => console.log(resp))
      .catch((err) => console.error(err))
  7. Use Promises with Vonage API methods

    main

    Most methods in the SDK that interact with Vonage APIs return Promises. You can handle these using async/await or by resolving the promises manually.

    const resp = await vonage.sms.send({
        to: '15552220000',
        from: '15559992222',
        text: 'This is a test',
    });