diff --git a/.env b/.env deleted file mode 100644 index 60c02f9..0000000 --- a/.env +++ /dev/null @@ -1,3 +0,0 @@ -SINCH_PROJECT_ID= -SINCH_KEY_ID= -SINCH_KEY_SECRET= \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6a05be7 --- /dev/null +++ b/.env.example @@ -0,0 +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= + +# 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/.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 17443cc..3d3f65c 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,58 @@ 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, ...) + +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 +``` + +**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 +``` + + +## 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..272324a 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 = "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( - 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..4ed73dc 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() @@ -19,24 +17,16 @@ key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET" ) +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" -} -voice_configuration: VoiceConfigurationDictType = { - "app_id": "APP_ID", - "type": "RTC" -} -number_pattern: NumberPatternDict = { - "pattern": "+1234", - "search_pattern": "START" + "service_plan_id": service_plan_id_to_associate_with_the_number } + 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)