From d5514c54e4e9810767898cbb75371e199c3d9b27 Mon Sep 17 00:00:00 2001 From: Jessica Matsuoka Date: Mon, 30 Jun 2025 10:48:35 +0200 Subject: [PATCH 1/5] DEVEXP-963: Numbers Snippets update --- .env | 11 ++++++- README.md | 29 +++++++++++++++++++ pyproject.toml | 15 ++++++++++ .../numbers/active_numbers/get/snippet.py | 2 +- .../numbers/active_numbers/release/snippet.py | 2 +- .../numbers/active_numbers/update/snippet.py | 15 +++------- .../check_availability/snippet.py | 2 +- .../numbers/available_numbers/rent/snippet.py | 6 ++-- .../available_numbers/rent_any/snippet.py | 16 +++------- 9 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 pyproject.toml diff --git a/.env b/.env index 60c02f9..6a05be7 100644 --- a/.env +++ b/.env @@ -1,3 +1,12 @@ +# The project ID where are defined the resources you want to use. SINCH_PROJECT_ID= + +# The API key ID and secret to authenticate your requests to the Sinch API. SINCH_KEY_ID= -SINCH_KEY_SECRET= \ No newline at end of file +SINCH_KEY_SECRET= + +# The virtual phone number you have rented from Sinch or planning to rent. +SINCH_PHONE_NUMBER= + +# The service plan ID for your Sinch account to configure the SMS plan associated with your virtual phone number. +SINCH_SERVICE_PLAN_ID= \ No newline at end of file diff --git a/README.md b/README.md index 17443cc..377a9d1 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,32 @@ Sinch Python SDK Code Snippets Repository This repository contains code snippets demonstrating usage of the [Sinch Python SDK](https://github.com/sinch/sinch-sdk-python). + +## Requirements +- Python 3.9 or later +- [Poetry](https://python-poetry.org/) for dependency management +- [Sinch account](https://dashboard.sinch.com) +- [Sinch package](https://pypi.org/project/sinch/) + + +## Snippets execution settings +When executing a snippet, you will need to provide some information about your Sinch account (credentials, Sinch virtual phone number, ...) + +This setting can be placed directly in the snippet source, or you can use an [environment file](.env), in which case the settings will be shared and used automatically by every snippet. + +Install dependencies using Poetry: + +```bash +poetry install +``` + + +## Running snippets + +All available code snippets are located in the `snippets/` directory, structured by feature and corresponding actions. + +To execute a specific snippet, navigate to the appropriate subdirectory and run: + +```shell +python run python snippet.py +``` \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..819eac3 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "sinch-sdk-python-snippets" +version = "0.1.0" +description = "Code snippets demonstrating usage of the Sinch Python SDK" +readme = "README.md" +packages = [{include = "snippets"}] + +[tool.poetry.dependencies] +python = "^3.9" +python-dotenv = "^1.0.0" +# sinch = "^2.0.0" # Uncomment once v2.0 is released + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" \ No newline at end of file diff --git a/snippets/numbers/active_numbers/get/snippet.py b/snippets/numbers/active_numbers/get/snippet.py index 0dfadbe..437694e 100644 --- a/snippets/numbers/active_numbers/get/snippet.py +++ b/snippets/numbers/active_numbers/get/snippet.py @@ -16,7 +16,7 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -phone_number = "MY_SINCH_PHONE_NUMBER" +phone_number = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER" response = sinch_client.numbers.get(phone_number=phone_number) print(f"Rented number details:\n{response}") diff --git a/snippets/numbers/active_numbers/release/snippet.py b/snippets/numbers/active_numbers/release/snippet.py index 830fc03..127a1c8 100644 --- a/snippets/numbers/active_numbers/release/snippet.py +++ b/snippets/numbers/active_numbers/release/snippet.py @@ -16,7 +16,7 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -phone_number = "PHONE_NUMBER_TO_BE_RELEASED" +phone_number = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER" released_number = sinch_client.numbers.release( phone_number=phone_number ) diff --git a/snippets/numbers/active_numbers/update/snippet.py b/snippets/numbers/active_numbers/update/snippet.py index 3762d5d..f01a07a 100644 --- a/snippets/numbers/active_numbers/update/snippet.py +++ b/snippets/numbers/active_numbers/update/snippet.py @@ -7,7 +7,6 @@ import os from dotenv import load_dotenv from sinch import SinchClient -from sinch.domains.numbers.models.v1.types import VoiceConfigurationDictType load_dotenv() @@ -17,18 +16,12 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -phone_number = "PHONE_NUMBER" -app_id = "APP_ID" -display_name = "DISPLAY_NAME" -voice_configuration: VoiceConfigurationDictType = { - "app_id": app_id, - "type": "RTC" -} +phone_number_to_update = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER" +updated_display_name = "Updated DISPLAY_NAME" response = sinch_client.numbers.update( - phone_number=phone_number, - display_name=display_name, - voice_configuration=voice_configuration + phone_number=phone_number_to_update, + display_name=updated_display_name ) print("Updated Number:\n", response) diff --git a/snippets/numbers/available_numbers/check_availability/snippet.py b/snippets/numbers/available_numbers/check_availability/snippet.py index 50f623a..197e31d 100644 --- a/snippets/numbers/available_numbers/check_availability/snippet.py +++ b/snippets/numbers/available_numbers/check_availability/snippet.py @@ -21,4 +21,4 @@ phone_number=phone_number ) -print("Released Number:\n", response) +print("The phone number is available:\n", response) diff --git a/snippets/numbers/available_numbers/rent/snippet.py b/snippets/numbers/available_numbers/rent/snippet.py index 2b10ca4..630d14a 100644 --- a/snippets/numbers/available_numbers/rent/snippet.py +++ b/snippets/numbers/available_numbers/rent/snippet.py @@ -17,14 +17,14 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -phone_number = "AVAILABLE_PHONE_NUMBER_TO_BE_RENTED" -service_plan_id = "SERVICE_PLAN_ID" +phone_number_to_be_rented = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER" +service_plan_id = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" sms_configuration: SmsConfigurationDict = { "service_plan_id": service_plan_id } rented_number = sinch_client.numbers.rent( - phone_number=phone_number, + phone_number=phone_number_to_be_rented, sms_configuration=sms_configuration ) print("Rented Number:\n", rented_number) diff --git a/snippets/numbers/available_numbers/rent_any/snippet.py b/snippets/numbers/available_numbers/rent_any/snippet.py index 3b075ce..9c3cbba 100644 --- a/snippets/numbers/available_numbers/rent_any/snippet.py +++ b/snippets/numbers/available_numbers/rent_any/snippet.py @@ -19,24 +19,16 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) +service_plan_id = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" sms_configuration: SmsConfigurationDict = { - "service_plan_id": "SERVICE_PLAN_ID" -} -voice_configuration: VoiceConfigurationDictType = { - "app_id": "APP_ID", - "type": "RTC" -} -number_pattern: NumberPatternDict = { - "pattern": "+1234", - "search_pattern": "START" + "service_plan_id": service_plan_id } + response = sinch_client.numbers.rent_any( region_code="US", type_="LOCAL", capabilities=["SMS", "VOICE"], - sms_configuration=sms_configuration, - voice_configuration=voice_configuration, - number_pattern=number_pattern + sms_configuration=sms_configuration ) print("Rented Number:\n", response) From 283177f3b4800916e2b466fd38d7b5c437ddaf06 Mon Sep 17 00:00:00 2001 From: Jessica Matsuoka Date: Mon, 30 Jun 2025 12:56:55 +0200 Subject: [PATCH 2/5] fix ci --- snippets/numbers/available_numbers/rent_any/snippet.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/numbers/available_numbers/rent_any/snippet.py b/snippets/numbers/available_numbers/rent_any/snippet.py index 9c3cbba..ea4a682 100644 --- a/snippets/numbers/available_numbers/rent_any/snippet.py +++ b/snippets/numbers/available_numbers/rent_any/snippet.py @@ -7,9 +7,7 @@ import os from dotenv import load_dotenv from sinch import SinchClient -from sinch.domains.numbers.models.v1.types import ( - NumberPatternDict, SmsConfigurationDict, VoiceConfigurationDictType -) +from sinch.domains.numbers.models.v1.types import SmsConfigurationDict load_dotenv() From caf8c426c2e8e06be6b25789310698d6a9c441da Mon Sep 17 00:00:00 2001 From: Jessica Matsuoka Date: Tue, 1 Jul 2025 16:08:37 +0200 Subject: [PATCH 3/5] fix comments --- .env => .env.example | 0 .gitignore | 1 + README.md | 7 ++++++- snippets/numbers/available_numbers/rent/snippet.py | 6 +++--- 4 files changed, 10 insertions(+), 4 deletions(-) rename .env => .env.example (100%) diff --git a/.env b/.env.example similarity index 100% rename from .env rename to .env.example diff --git a/.gitignore b/.gitignore index 3f5ea3e..a3451bf 100644 --- a/.gitignore +++ b/.gitignore @@ -93,6 +93,7 @@ celerybeat.pid *.sage.py # Environments +.env .venv env/ venv/ diff --git a/README.md b/README.md index 377a9d1..4628e87 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,12 @@ This repository contains code snippets demonstrating usage of the ## Snippets execution settings When executing a snippet, you will need to provide some information about your Sinch account (credentials, Sinch virtual phone number, ...) -This setting can be placed directly in the snippet source, or you can use an [environment file](.env), in which case the settings will be shared and used automatically by every snippet. +This setting can be placed directly in the snippet source, or you can use an [environment file](.env.example), in which case the settings will be shared and used automatically by every snippet. +Rename the `.env.example` file to `.env` and fill in the required values: + +```bash +cp .env.example .env +``` Install dependencies using Poetry: diff --git a/snippets/numbers/available_numbers/rent/snippet.py b/snippets/numbers/available_numbers/rent/snippet.py index 630d14a..272324a 100644 --- a/snippets/numbers/available_numbers/rent/snippet.py +++ b/snippets/numbers/available_numbers/rent/snippet.py @@ -17,10 +17,10 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -phone_number_to_be_rented = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER" -service_plan_id = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" +phone_number_to_be_rented = "AVAILABLE_PHONE_NUMBER_TO_BE_RENTED" +service_plan_id_to_associate_with_the_number = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" sms_configuration: SmsConfigurationDict = { - "service_plan_id": service_plan_id + "service_plan_id": service_plan_id_to_associate_with_the_number } rented_number = sinch_client.numbers.rent( From 003263f3f861658ff99e34261ecd554043ce09bd Mon Sep 17 00:00:00 2001 From: Jessica Matsuoka Date: Tue, 1 Jul 2025 17:36:19 +0200 Subject: [PATCH 4/5] update rent any --- snippets/numbers/available_numbers/rent_any/snippet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/numbers/available_numbers/rent_any/snippet.py b/snippets/numbers/available_numbers/rent_any/snippet.py index ea4a682..4ed73dc 100644 --- a/snippets/numbers/available_numbers/rent_any/snippet.py +++ b/snippets/numbers/available_numbers/rent_any/snippet.py @@ -17,9 +17,9 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) -service_plan_id = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" +service_plan_id_to_associate_with_the_number = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID" sms_configuration: SmsConfigurationDict = { - "service_plan_id": service_plan_id + "service_plan_id": service_plan_id_to_associate_with_the_number } response = sinch_client.numbers.rent_any( From 16c5b27447d843f73c0074dbfe0cb2e9b116166c Mon Sep 17 00:00:00 2001 From: Jessica Matsuoka Date: Wed, 2 Jul 2025 14:36:40 +0200 Subject: [PATCH 5/5] Update README --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4628e87..3d3f65c 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,35 @@ This repository contains code snippets demonstrating usage of the ## Snippets execution settings When executing a snippet, you will need to provide some information about your Sinch account (credentials, Sinch virtual phone number, ...) -This setting can be placed directly in the snippet source, or you can use an [environment file](.env.example), in which case the settings will be shared and used automatically by every snippet. -Rename the `.env.example` file to `.env` and fill in the required values: +These settings can be placed directly in the snippet source code, **or** you can use an environment file (`.env`). Using an environment file allows the settings to be shared and used automatically by every snippet. +### Setting Up Your Environment File + +#### 1. Rename the example file + +**Linux / Mac:** ```bash cp .env.example .env ``` -Install dependencies using Poetry: +**Windows (Command Prompt):** +```cmd +copy .env.example .env +``` + +Windows (PowerShell): +```powershell +Copy-Item .env.example .env +``` + +#### 2. Fill in your credentials + +Open the newly created [.env](.env) file in your preferred text editor and fill in the required values (e.g., SINCH_PROJECT_ID=your_project_id). + +Note: Do not share your .env file or credentials publicly. + + +### Install dependencies using Poetry: ```bash poetry install