vapor-community / VaporTwilioService

Twilio API provider for all your Vapor needs
19 stars 12 forks source link

Method to lookup for a phone number #12

Closed seeppp closed 2 days ago

seeppp commented 3 years ago

Twilio offers an endpoint to lookup phone numbers. https://www.twilio.com/docs/lookup/quickstart

A new method lookup(number:) in the Twilio module will use the Twilio Endpoint to lookup the given phone number.

import Twilio

func routes(_ app: Application) throws {
    app.get("lookup") { req -> EventLoopFuture<LookupResponse> in
        let phoneNumber = "+18316100806"
        return req.twilio.lookup(phoneNumber)
    }
}

If successful, the LoopupResponse will look like this:

{
    "countryCode": "US",
    "nationalFormat": "(831) 610-0806",
    "carrier": {
        "name": "Twilio - Inteliquent - SMS-Sybase365/MMS-SVR",
        "type": "voip"
    },
    "phoneNumber": "+18316100806"
}

The carrier.type can either be voip, mobile or landline. Which is very handy to check if a number can receive SMS.