Skip to content

Commit 6dc4c64

Browse files
committed
update account snippets for v4, add subaccounts snippets
1 parent 691d43e commit 6dc4c64

24 files changed

+325
-107
lines changed

.env.dist

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ MESSAGES_SANDBOX_VIBER_SERVICE_ID=
3131
VONAGE_SIGNATURE_SECRET=
3232
VONAGE_SIGNATURE_SECRET_METHOD=
3333

34-
# Verify v2
34+
# Verify
3535
TO_EMAIL='EMAIL_ADDRESS_TO_VERIFY'
3636
REQUEST_ID='REQUEST_ID'
3737
BRAND_NAME='Acme. Inc'
3838
CODE='CODE_SENT_TO_THE_USER'
3939

40-
# Verify
40+
# Verify (Legacy)
4141
WORKFLOW_ID=1
4242

4343
# Number Insight
@@ -53,7 +53,15 @@ BRAND_TEXT='YOUR_BRAND_NAME'
5353

5454
# Account
5555
SMS_CALLBACK_URL='YOUR_CALLBACK_URL'
56-
VONAGE_SECRET_ID='YOUR_VONAGE_SECRET_ID'
56+
NEW_SECRET='YOUR_NEW_SECRET_ID'
57+
VONAGE_SECRET_ID='YOUR_SECRET_ID'
58+
59+
# Subaccounts
60+
NEW_SUBACCOUNT_NAME='NEW_SUBACCOUNT_NAME'
61+
NEW_SUBACCOUNT_SECRET='NEW_SUBACCOUNT_SECRET'
62+
SUBACCOUNT_KEY='SUBACCOUNT_KEY'
63+
AMOUNT=0.00
64+
START_DATE='START_DATE'
5765

5866
# Voice
5967
CONFERENCE_NAME='NAME_OF_YOUR_CONFERENCE'
@@ -68,8 +76,4 @@ VONAGE_NUMBER_FEATURES='SMS,VOICE'
6876
MESSAGES_APPLICATION_ID='12a3b4cd-a1b2-1ab2-a1b2-a1234bc5d678'
6977
VOICE_CALLBACK_TYPE='tel'
7078
VOICE_CALLBACK_VALUE='447700900002'
71-
VOICE_STATUS_URL='https://example.com/webhooks/status'
72-
73-
# Redact
74-
VONAGE_REDACT_ID=
75-
VONAGE_REDACT_TYPE=
79+
VOICE_STATUS_URL='https://example.com/webhooks/status'

.github/workflows/linter.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ jobs:
2828
runs-on: ubuntu-latest
2929

3030
steps:
31-
- uses: actions/checkout@v2
32-
- name: Set up Python 3.10
33-
uses: actions/setup-python@v4
31+
- uses: actions/checkout@v4
32+
- name: Set up Python 3.13
33+
uses: actions/setup-python@v5
3434
with:
35-
python-version: '3.10'
35+
python-version: '3.13'
3636
- name: Install dependencies
3737
run: |
3838
python -m pip install --upgrade pip

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ celerybeat-schedule
8181
# virtualenv
8282
venv*/
8383
ENV/
84+
.venv*/
8485

8586
# Spyder project settings
8687
.spyderproject

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2022 Vonage Community
3+
Copyright (c) 2024 Vonage Community
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
![Last Commit](https://img.shields.io/github/last-commit/Vonage/vonage-python-code-snippets)
99
![Size](https://img.shields.io/github/repo-size/Vonage/vonage-python-code-snippets)
1010

11-
<img src="https://developer.nexmo.com/assets/images/Vonage_Nexmo.svg" height="48px" alt="Nexmo is now known as Vonage" />
12-
13-
Code snippets also available for: [Java](https://github.com/nexmo/nexmo-java-code-snippets), [.NET](https://github.com/nexmo/nexmo-dotnet-code-snippets), [Node.js](https://github.com/nexmo/nexmo-node-code-snippets), [PHP](https://github.com/nexmo/nexmo-php-code-snippets), [Ruby](https://github.com/nexmo/nexmo-ruby-code-snippets), [Curl](https://github.com/Nexmo/nexmo-curl-code-snippets), [Go](https://github.com/Vonage/vonage-go-code-snippets) and [Android](https://github.com/nexmo-community/quickstart-android)
14-
15-
The purpose of the Code Snippets is to provide simple examples focused
16-
on one goal. For example, sending an SMS, handling an incoming SMS webhook,
17-
or making a Text to Speech call.
11+
The purpose of these Code Snippets is to provide simple examples focused
12+
on one goal. For example, sending an SMS, creating a Vonage Video API session, handling an incoming webhook, or making a Text-to-Speech call.
1813

1914
## Setup
2015

21-
These code samples are meant to be used for [https://developer.vonage.com/](https://developer.vonage.com/), and are structured in such a way as to be used for internal testing. Developers are free to use these code snippets as a reference, but these may require changes to be worked into your specific application. We recommend checking out the [Vonage Developer Website](https://developer.vonage.com/), which displays these code snippets in a more copy/paste fashion.
16+
These code samples are meant to be embedded into pages on [https://developer.vonage.com/](https://developer.vonage.com/). Developers are free to use these code snippets as a reference, but these may require changes to be worked into your specific application. We recommend checking out the [Vonage Developer Website](https://developer.vonage.com/), which displays these code snippets in a more copy/paste fashion.
2217

2318
To use the examples, you will first need a [Vonage account][sign-up]. Then rename
2419
the `.env.dist` file to `.env` and set the values as required.

account/configure-account.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
from os.path import join, dirname
33
from pprint import pprint
4-
import vonage
54
from dotenv import load_dotenv
65

76
dotenv_path = join(dirname(__file__), '../.env')
@@ -11,7 +10,13 @@
1110
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
1211
SMS_CALLBACK_URL = os.getenv('SMS_CALLBACK_URL')
1312

14-
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
13+
from vonage import Auth, Vonage
14+
from vonage_account import SettingsResponse
1515

16-
result = client.account.update_default_sms_webhook({'moCallBackUrl':SMS_CALLBACK_URL})
17-
pprint(result)
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
17+
18+
settings: SettingsResponse = client.account.update_default_sms_webhook(
19+
mo_callback_url=SMS_CALLBACK_URL
20+
)
21+
22+
print(settings)

account/create-secret.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
NEW_SECRET = os.getenv('NEW_SECRET')
12+
13+
from vonage import Auth, Vonage
14+
from vonage_account import VonageApiSecret
15+
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
17+
18+
response: VonageApiSecret = client.account.create_secret(NEW_SECRET)
19+
print(response)
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from os.path import join, dirname
3-
import vonage
43
from dotenv import load_dotenv
54

65
dotenv_path = join(dirname(__file__), "../.env")
@@ -9,7 +8,12 @@
98
VONAGE_API_KEY = os.environ.get("VONAGE_API_KEY")
109
VONAGE_API_SECRET = os.environ.get("VONAGE_API_SECRET")
1110
VONAGE_SECRET_ID = os.environ.get("VONAGE_SECRET_ID")
12-
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
1311

14-
secret = client.account.get_secret(api_key=VONAGE_API_KEY, secret_id=VONAGE_SECRET_ID)
15-
print(secret["id"] + ": Created on " + secret["created_at"])
12+
from vonage import Auth, Vonage
13+
from vonage_account import VonageApiSecret
14+
15+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
16+
17+
secret: VonageApiSecret = client.account.get_secret(VONAGE_SECRET_ID)
18+
19+
print(f'Secret ID: {secret.id}; Created at {secret.created_at}')

account/get-balance.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import os
22
from os.path import join, dirname
33
from dotenv import load_dotenv
4-
import vonage
54

65
dotenv_path = join(dirname(__file__), '../.env')
76
load_dotenv(dotenv_path)
87

98
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
109
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
1110

12-
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
11+
from vonage import Auth, Vonage
12+
from vonage_account import Balance
1313

14-
result = client.account.get_balance()
15-
print(f"{result['value']:0.2f} EUR")
14+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
15+
16+
balance: Balance = client.account.get_balance()
17+
18+
print(f'{balance.value:0.2f} EUR, auto-reload: {balance.auto_reload}')

account/list-all-secrets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
dotenv_path = join(dirname(__file__), "../.env")
6+
load_dotenv(dotenv_path)
7+
8+
VONAGE_API_KEY = os.environ.get("VONAGE_API_KEY")
9+
VONAGE_API_SECRET = os.environ.get("VONAGE_API_SECRET")
10+
11+
from vonage import Auth, Vonage
12+
from vonage_account import VonageApiSecret
13+
14+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
15+
16+
response: list[VonageApiSecret] = client.account.list_secrets()
17+
print(response)

account/revoke-secret.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.environ.get("VONAGE_API_KEY")
10+
VONAGE_API_SECRET = os.environ.get("VONAGE_API_SECRET")
11+
VONAGE_SECRET_ID = os.getenv("VONAGE_SECRET_ID")
12+
13+
from vonage import Auth, Vonage
14+
15+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
16+
client.account.revoke_secret(VONAGE_SECRET_ID)

secrets/create-secret.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

secrets/list-all-secrets.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

secrets/revoke-secret.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

subaccounts/create-subaccount.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
NEW_SUBACCOUNT_NAME = os.getenv('NEW_SUBACCOUNT_NAME')
12+
NEW_SUBACCOUNT_SECRET = os.getenv('NEW_SUBACCOUNT_SECRET')
13+
14+
from vonage import Auth, Vonage
15+
from vonage_subaccounts import NewSubaccount, SubaccountOptions
16+
17+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
18+
19+
response: NewSubaccount = client.subaccounts.create_subaccount(
20+
SubaccountOptions(
21+
name=NEW_SUBACCOUNT_NAME,
22+
secret=NEW_SUBACCOUNT_SECRET,
23+
use_primary_account_balance=False,
24+
)
25+
)
26+
27+
print(response)

subaccounts/get-subaccount.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
SUBACCOUNT_KEY = os.getenv('SUBACCOUNT_KEY')
12+
13+
from vonage import Auth, Vonage
14+
from vonage_subaccounts import Subaccount
15+
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
17+
18+
subaccount: Subaccount = client.subaccounts.get_subaccount(SUBACCOUNT_KEY)
19+
20+
print(subaccount)

subaccounts/list-balance-transfers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
START_DATE = os.getenv("START_DATE")
12+
13+
from vonage import Auth, Vonage
14+
from vonage_subaccounts import ListTransfersFilter, Transfer
15+
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
17+
18+
response: list[Transfer] = client.subaccounts.list_balance_transfers(
19+
ListTransfersFilter(start_date=START_DATE)
20+
)
21+
22+
print(response)

subaccounts/list-credit-transfers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
# Load the environment
6+
envpath = join(dirname(__file__), "../.env")
7+
load_dotenv(envpath)
8+
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
START_DATE = os.getenv("START_DATE")
12+
13+
from vonage import Auth, Vonage
14+
from vonage_subaccounts import ListTransfersFilter, Transfer
15+
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
17+
18+
response: list[Transfer] = client.subaccounts.list_credit_transfers(
19+
ListTransfersFilter(start_date=START_DATE)
20+
)
21+
22+
print(response)

0 commit comments

Comments
 (0)