|
| 1 | +.. module::twilio.rest |
| 2 | +
|
| 3 | +====================================== |
| 4 | +Accessing Twilio Pricing API Resources |
| 5 | +====================================== |
| 6 | + |
| 7 | +To access the Twilio Pricing API resources, you'll first need to instantiate |
| 8 | +a :class:`TwilioPricingClient`. |
| 9 | + |
| 10 | +Authentication |
| 11 | +-------------- |
| 12 | + |
| 13 | +The :class:`TwilioPricingClient` needs your Twilio credentials. While these can be |
| 14 | +passed in directly to the constructor, we suggest storing your credentials as |
| 15 | +environment variables. Why? You'll never have to worry about committing your |
| 16 | +credentials and accidentally posting them somewhere public. |
| 17 | + |
| 18 | +The :class:`TwilioPricingClient` looks for :const:`TWILIO_ACCOUNT_SID` and |
| 19 | +:const:`TWILIO_AUTH_TOKEN` inside the current environment. |
| 20 | + |
| 21 | +With those two values set, create a new :class:`TwilioPricingClient`. |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + from twilio.rest import TwilioPricingClient |
| 26 | +
|
| 27 | + conn = TwilioPricingClient() |
| 28 | +
|
| 29 | +If you'd rather not use environment variables, pass your account credentials |
| 30 | +directly to the the constructor. |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + from twilio.rest import TwilioPricingClient |
| 35 | +
|
| 36 | + ACCOUNT_SID = "AXXXXXXXXXXXXXXXXX" |
| 37 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 38 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 39 | +
|
| 40 | +
|
| 41 | +For more advanced options to use with the :class:`TwilioPricingClient`, |
| 42 | +including proxy configuration, see the :doc:`/usage/basics` page. |
| 43 | +:class:`TwilioPricingClient` accepts the same options as |
| 44 | +:class:`TwilioRestClient`. |
| 45 | + |
| 46 | +============= |
| 47 | +Voice Pricing |
| 48 | +============= |
| 49 | + |
| 50 | +Twilio Voice pricing is available by country and by phone number. |
| 51 | + |
| 52 | +Voice calls are priced per minute and reflect the current Twilio list price |
| 53 | +and any discounts available to the requesting account at the time of the |
| 54 | +request. |
| 55 | + |
| 56 | +Voice Countries |
| 57 | +--------------- |
| 58 | + |
| 59 | +To retrieve a list of countries where Twilio Voice services are available: |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + from twilio.rest import TwilioPricingClient |
| 64 | +
|
| 65 | + # To find these visit https://www.twilio.com/user/account |
| 66 | + ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX" |
| 67 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 68 | +
|
| 69 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 70 | + countries = client.voice.countries.list() |
| 71 | + for c in countries: |
| 72 | + print c.iso_country |
| 73 | +
|
| 74 | +Note that the returned list of countries will not have actual prices populated. |
| 75 | +You will need to retrieve the pricing information for each country you are |
| 76 | +interested in individually: |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | +
|
| 80 | + from twilio.rest import TwilioPricingClient |
| 81 | +
|
| 82 | + # To find these visit https://www.twilio.com/user/account |
| 83 | + ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX" |
| 84 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 85 | +
|
| 86 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 87 | + country = client.voice.countries.get('GB') |
| 88 | + print country.iso_country |
| 89 | + print country.price_unit |
| 90 | +
|
| 91 | + # A list of price rates for inbound calls to each type of Twilio |
| 92 | + # Number available in this country |
| 93 | + for p in country.inbound_call_prices: |
| 94 | + print p.number_type |
| 95 | + print p.call_base_price # Base price per minute |
| 96 | + print p.current_base_price # Price per minute after discounts |
| 97 | +
|
| 98 | + # A list of price rates for outbound calls, organized by number prefixes. |
| 99 | + for p in country.outbound_prefix_prices: |
| 100 | + print p.prefixes # List of one or more prefixes this price applies to |
| 101 | + print p.call_base_price # Base price per minute |
| 102 | + print p.current_base_price # Price per minute after discounts |
| 103 | +
|
| 104 | +Voice Numbers |
| 105 | +------------- |
| 106 | + |
| 107 | +To retrieve pricing information for Twilio Voice calls to and from a specific |
| 108 | +number: |
| 109 | + |
| 110 | +.. code-block:: python |
| 111 | +
|
| 112 | + from twilio.rest import TwilioPricingClient |
| 113 | +
|
| 114 | + # To find these visit https://www.twilio.com/user/account |
| 115 | + ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX" |
| 116 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 117 | +
|
| 118 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 119 | + number = client.voice.numbers.get('+15105551234') |
| 120 | +
|
| 121 | + print number.iso_country |
| 122 | + print number.price_unit |
| 123 | + print number.outbound_call_price.call_base_price |
| 124 | + print number.inbound_call_price.call_base_price # None if the number is not Twilio-hosted |
| 125 | +
|
| 126 | +Phone Number Pricing |
| 127 | +==================== |
| 128 | + |
| 129 | +To retrieve a list of countries where Twilio phone numbers are available: |
| 130 | + |
| 131 | +.. code-block:: python |
| 132 | +
|
| 133 | + from twilio.rest import TwilioPricingClient |
| 134 | +
|
| 135 | + # To find these visit https://www.twilio.com/user/account |
| 136 | + ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX" |
| 137 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 138 | +
|
| 139 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 140 | + countries = client.phone_numbers.countries.list() |
| 141 | + for c in countries: |
| 142 | + print c.iso_country |
| 143 | +
|
| 144 | +Note that the country objects in the returned list will not have pricing |
| 145 | +information populated; you will need to retrieve the specific information for |
| 146 | +each country you are interested in individually: |
| 147 | + |
| 148 | +.. code-block:: python |
| 149 | +
|
| 150 | + from twilio.rest import TwilioPricingClient |
| 151 | +
|
| 152 | + # To find these visit https://www.twilio.com/user/account |
| 153 | + ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX" |
| 154 | + AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY" |
| 155 | +
|
| 156 | + client = TwilioPricingClient(ACCOUNT_SID, AUTH_TOKEN) |
| 157 | + country = client.phone_numbers.countries.get('GB') |
| 158 | + print country.price_unit |
| 159 | +
|
| 160 | + for p in country.phone_number_prices: |
| 161 | + print p.type |
| 162 | + print p.base_price |
| 163 | + print p.current_price |
| 164 | +
|
0 commit comments