Skip to content

Commit f66f787

Browse files
committed
Adds documentation for the Lookups API
1 parent 3766d18 commit f66f787

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ capability.allow_client_incoming 'andrew'
147147
There is a slightly more detailed document in the [Capability][capability]
148148
section of the wiki.
149149

150+
## Lookup Phone Number information
151+
152+
You can look up details on phone numbers with the Lookups API.
153+
154+
```ruby
155+
require 'twilio-ruby'
156+
157+
# put your own credentials here
158+
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
159+
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
160+
161+
# set up a client to talk to the Twilio REST API
162+
@lookups_client = Twilio::REST::LookupsClient.new account_sid, auth_token
163+
164+
# lookup a number
165+
number = @lookups_client.phone_numbers.get('+14159341234')
166+
167+
# investigate the number
168+
number.national_format
169+
# => "(415) 934-1234"
170+
number.country_code
171+
# => "US"
172+
```
173+
150174
## Getting Started With TwiML
151175

152176
TwiML support is based on the [Builder][builder] library. You can construct a
@@ -191,7 +215,7 @@ implementations:
191215
- Ruby 1.9.3
192216
- [JRuby][jruby]
193217
- [Rubinius][rubinius]
194-
-
218+
-
195219

196220
## Getting help
197221

docs/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ state.
7070
usage/taskrouter-tokens
7171

7272

73+
Lookups
74+
-------
75+
76+
Query the Lookups API to get information about phone numbers and their carriers.
77+
78+
.. toctree::
79+
:maxdepth: 1
80+
81+
usage/lookups
82+
7383
TwiML
7484
---------
7585

docs/usage/lookups.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
===========
2+
Lookups API
3+
===========
4+
5+
Lookups allows you to systematically ascertain information about phone numbers. With Lookups, you can identify local-friendly number formats, reduce the likelihood of undelivered messages and protect yourself from fraud.
6+
7+
For more information see the `Lookups API <https://www.twilio.com/docs/api/rest/lookups>`_ documentation.
8+
9+
Looking up details on a phone number
10+
------------------------------------
11+
12+
You can look up a phone number with a :class:`Twilio::REST::LookupsClient`. You instantiate the client as you would with any other :class:`Twilio::REST` client
13+
14+
.. code-block:: ruby
15+
16+
require "twilio-ruby"
17+
18+
# Find these values at twilio.com/user/account
19+
account_sid = "AC123123"
20+
auth_token = "secret"
21+
22+
@lookups_client = Twilio::REST::LookupsClient.new account_sid, auth_token
23+
24+
You can then use the client to lookup a phone number.
25+
26+
.. code-block:: ruby
27+
28+
response = @lookups_client.phone_numbers.get("+12316851234")
29+
response.country_code
30+
# => "US"
31+
response.phone_number
32+
# => "+12316851234"
33+
response.national_format
34+
# => "(231) 685-1234"
35+
response.url
36+
# => "https://lookups.twilio.com/v1/PhoneNumbers/+12316851234"
37+
38+
Invalid Phone Numbers
39+
---------------------
40+
41+
The Lookups API is a REST API that returns data on phone number resources. If you try to lookup a phone number that doesn't exist the API will raise a 404 :class:`Twilio::REST::RequestError`. You should handle this within your code.
42+
43+
.. code-block:: ruby
44+
45+
response = @lookups_client.phone_numbers.get("+15558675309")
46+
begin
47+
puts response.phone_number
48+
rescue Twilio::REST::RequestError => e
49+
raise e unless e.code == 20404 # ensure this is a 404 error
50+
puts "Invalid number"
51+
end
52+
53+
Carrier Information
54+
-------------------
55+
56+
The Lookups API can be used to find out more information about the carrier for the phone number. Just pass the type "carrier" to the request.
57+
58+
.. code-block:: ruby
59+
60+
response = @lookups_client.phone_numbers.get("+12316851234", type: "carrier")
61+
response.carrier
62+
# => {"mobile_country_code"=>nil, "mobile_network_code"=>nil, "name"=>"Charter Fiberlink, LLC", "type"=>"landline", "error_code"=>nil}

0 commit comments

Comments
 (0)