Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .env

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generally preferable to not commit the .env file as someone could commit sensitive information by mistake.
Instead, create an example file and instruct the user with a command to rename it (add also a .gitignore)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

This file was deleted.

12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,37 @@ 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.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:

```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
```
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion snippets/numbers/active_numbers/get/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
2 changes: 1 addition & 1 deletion snippets/numbers/active_numbers/release/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
15 changes: 4 additions & 11 deletions snippets/numbers/active_numbers/update/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
phone_number=phone_number
)

print("Released Number:\n", response)
print("The phone number is available:\n", response)
8 changes: 4 additions & 4 deletions snippets/numbers/available_numbers/rent/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 5 additions & 15 deletions snippets/numbers/available_numbers/rent_any/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)