opensms 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opensms-0.1.0/.gitignore +17 -0
- opensms-0.1.0/LICENSE +21 -0
- opensms-0.1.0/PKG-INFO +285 -0
- opensms-0.1.0/README.md +263 -0
- opensms-0.1.0/pyproject.toml +41 -0
- opensms-0.1.0/src/opensms/__init__.py +30 -0
- opensms-0.1.0/src/opensms/_serialize.py +60 -0
- opensms-0.1.0/src/opensms/_transport.py +260 -0
- opensms-0.1.0/src/opensms/_version.py +3 -0
- opensms-0.1.0/src/opensms/client.py +127 -0
- opensms-0.1.0/src/opensms/errors.py +62 -0
- opensms-0.1.0/src/opensms/models.py +453 -0
- opensms-0.1.0/src/opensms/pagination.py +32 -0
- opensms-0.1.0/src/opensms/py.typed +0 -0
- opensms-0.1.0/src/opensms/resources/__init__.py +1 -0
- opensms-0.1.0/src/opensms/resources/_base.py +19 -0
- opensms-0.1.0/src/opensms/resources/analytics.py +95 -0
- opensms-0.1.0/src/opensms/resources/batches.py +116 -0
- opensms-0.1.0/src/opensms/resources/compliance.py +25 -0
- opensms-0.1.0/src/opensms/resources/contact_groups.py +83 -0
- opensms-0.1.0/src/opensms/resources/contacts.py +51 -0
- opensms-0.1.0/src/opensms/resources/countries.py +29 -0
- opensms-0.1.0/src/opensms/resources/inbound.py +28 -0
- opensms-0.1.0/src/opensms/resources/lookups.py +24 -0
- opensms-0.1.0/src/opensms/resources/messages.py +80 -0
- opensms-0.1.0/src/opensms/resources/numbers.py +96 -0
- opensms-0.1.0/src/opensms/resources/otp.py +42 -0
- opensms-0.1.0/src/opensms/resources/pricing.py +17 -0
- opensms-0.1.0/src/opensms/resources/sandbox.py +20 -0
- opensms-0.1.0/src/opensms/resources/sender_ids.py +160 -0
- opensms-0.1.0/src/opensms/resources/suppressions.py +41 -0
- opensms-0.1.0/src/opensms/resources/templates.py +52 -0
- opensms-0.1.0/src/opensms/resources/wallet.py +44 -0
- opensms-0.1.0/src/opensms/resources/webhooks.py +123 -0
- opensms-0.1.0/src/opensms/signature.py +120 -0
- opensms-0.1.0/tests/test_live.py +530 -0
- opensms-0.1.0/tests/test_unit.py +684 -0
- opensms-0.1.0/tests/test_urllib_transport.py +91 -0
opensms-0.1.0/.gitignore
ADDED
opensms-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 opensms
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
opensms-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: opensms
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python client for OpenSMS, the prepaid SMS API: send messages, OTPs, batches and lookups across Africa.
|
|
5
|
+
Project-URL: Homepage, https://opensms.io
|
|
6
|
+
Project-URL: Repository, https://github.com/opensms-io/opensms-sdks
|
|
7
|
+
Project-URL: Issues, https://github.com/opensms-io/opensms-sdks/issues
|
|
8
|
+
Author: OpenSMS
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: africa,api,messaging,opensms,otp,sms
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Communications :: Telephony
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# opensms (Python)
|
|
24
|
+
|
|
25
|
+
Official Python client for [opensms](https://opensms.io): prepaid SMS for Africa.
|
|
26
|
+
|
|
27
|
+
Pure standard library: no runtime dependencies. Python 3.8+. Fully typed (`py.typed`).
|
|
28
|
+
See the [monorepo README](https://github.com/opensms-io/opensms-sdks) for the other SDKs and
|
|
29
|
+
[spec/SURFACE.md](https://github.com/opensms-io/opensms-sdks/blob/main/spec/SURFACE.md) for the full API surface.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install opensms
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Not yet published to PyPI: until it is, install from this repo with `pip install ./packages/python` (run from the `sdks` directory), or `pip install -e ./packages/python` for an editable install.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from opensms import Opensms
|
|
43
|
+
|
|
44
|
+
client = Opensms(api_key="sk_test_...")
|
|
45
|
+
|
|
46
|
+
message = client.messages.send(to="+254712345678", text="Your order has shipped")
|
|
47
|
+
print(message["id"], message["status"], message["price"]) # price is a decimal string
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
A key starting `sk_test_` selects the sandbox, `sk_live_` the live environment; `client.environment`
|
|
51
|
+
reports which one is active, and a malformed key raises `ValueError` before any request is made.
|
|
52
|
+
Other constructor options: `base_url` (default `https://api.opensms.io`), `timeout` (seconds per
|
|
53
|
+
attempt, default 30), `max_retries` (default 2), and `transport` (swap the network layer for tests).
|
|
54
|
+
|
|
55
|
+
## More
|
|
56
|
+
|
|
57
|
+
One short example per resource, in the order they're listed on `client`.
|
|
58
|
+
|
|
59
|
+
### messages
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
m = client.messages.send(to="+254712345678", text="Hello", sender_id="ACME")
|
|
63
|
+
client.messages.get(m["id"])
|
|
64
|
+
client.messages.cancel(m["id"]) # only queued or scheduled messages
|
|
65
|
+
page = client.messages.list(limit=50, status="delivered", country="KE")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### batches
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
b = client.batches.create(items=[
|
|
72
|
+
{"to": "+254712345678", "text": "Hi Ada"},
|
|
73
|
+
])
|
|
74
|
+
client.batches.start(b["id"]) # batches are created "ready"
|
|
75
|
+
client.batches.get(b["id"])
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### otp
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
otp = client.otp.send(to="+254712345678", length=6, ttl_seconds=300)
|
|
82
|
+
result = client.otp.verify(otp_id=otp["otp_id"], code="123456")
|
|
83
|
+
print(result["valid"], result["attempts_left"])
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### lookups
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
lk = client.lookups.create(to="+254712345678")
|
|
90
|
+
client.lookups.get(lk["id"])
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### contacts
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
ct = client.contacts.create(e164="+254712345678", name="Ada")
|
|
97
|
+
client.contacts.update(ct["id"], name="Ada L") # PATCH: other fields kept
|
|
98
|
+
client.contacts.list(limit=200)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### contact_groups
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
g = client.contact_groups.create(name="VIP", contact_ids=[ct["id"]])
|
|
105
|
+
client.contact_groups.send(g["id"], text="Sale starts today")
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### templates
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
tpl = client.templates.create(name="welcome", body="Hi {{name}}", traffic_type="transactional")
|
|
112
|
+
client.templates.update(tpl["id"], body="Hello {{name}}")
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### webhooks
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
wh = client.webhooks.create(url="https://example.com/opensms", events=["message.delivered"])
|
|
119
|
+
secret = wh["secret"] # whsec_..., shown only once
|
|
120
|
+
client.webhooks.update(wh["id"], url=wh["url"], events=wh["events"], enabled=True) # full replacement
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### inbound
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
client.inbound.list()
|
|
127
|
+
client.inbound.reply(inbound_id, text="Thanks!") # live keys only
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### numbers
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
n = client.numbers.assign(country="KE", kind="long_code") # live keys only, charges the wallet
|
|
134
|
+
client.numbers.create_rule(n["id"], match="keyword", pattern="STOP", action="webhook", target="https://example.com/in")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### sender_ids
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
draft = client.sender_ids.create_draft(source="application", value="ACME", kind="alphanumeric", countries=["KE"])
|
|
141
|
+
sid = client.sender_ids.create(value="ACME", kind="alphanumeric", countries=["KE"],
|
|
142
|
+
documents=[], use_case="transactional") # may charge fees; never auto-retried
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### suppressions
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
s = client.suppressions.create(e164="+254712345678", reason="manual")
|
|
149
|
+
client.suppressions.import_([{"e164": "+254712345679", "reason": "complaint"}])
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`import` is a Python keyword, so the method is `import_` (`getattr(client.suppressions, "import")` also works).
|
|
153
|
+
|
|
154
|
+
### compliance
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
client.compliance.list_countries()
|
|
158
|
+
client.compliance.get_country("KE")
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### wallet
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
client.wallet.balances()
|
|
165
|
+
client.wallet.ledger(limit=50) # not cursor based; page with before=<smallest id seen>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### pricing
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
client.pricing.get(product="sms", country="KE")
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### analytics
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
client.analytics.overview(range="7d")
|
|
178
|
+
client.analytics.by_country(from_="2026-09-01", to="2026-09-24") # from is a keyword, so from_
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### sandbox
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
client.sandbox.list_messages(limit=10) # rendered texts of sandbox sends, including OTP codes
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### countries
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
client.countries.list()
|
|
191
|
+
client.countries.carriers("KE")
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Pagination
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
for message in client.paginate(client.messages.list, limit=50):
|
|
198
|
+
print(message["id"])
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
`client.paginate(list_method, *args, **params)` feeds each page's `next_cursor` back as `cursor` until
|
|
202
|
+
it is `None`. Bare-array endpoints and `wallet.ledger` are not paginated by it.
|
|
203
|
+
|
|
204
|
+
## Errors and retries
|
|
205
|
+
|
|
206
|
+
Every non-2xx response, and any network failure or timeout that survives all retries, raises
|
|
207
|
+
`OpensmsError`:
|
|
208
|
+
|
|
209
|
+
| Field | Notes |
|
|
210
|
+
|---|---|
|
|
211
|
+
| `status` | HTTP status; `0` means a network error, timeout, or webhook signature failure |
|
|
212
|
+
| `message` | `detail`, else `title`, else a generic message |
|
|
213
|
+
| `type` | problem `type` URI, usually `about:blank` |
|
|
214
|
+
| `title` | problem `title`, e.g. `"Bad Request"` |
|
|
215
|
+
| `detail` | the specific human readable reason |
|
|
216
|
+
| `code` | machine readable code; absent on most errors |
|
|
217
|
+
| `trace_id` | present when the server sets one |
|
|
218
|
+
| `errors` | field validation errors, `{field: [messages]}`, when present |
|
|
219
|
+
| `request_id` | `X-Request-ID`; set on message and OTP admission rejections |
|
|
220
|
+
| `retry_after` | seconds, from `Retry-After`, when the server sent it |
|
|
221
|
+
| `body` | raw decoded body (dict), or text when the body was not JSON |
|
|
222
|
+
|
|
223
|
+
Most errors carry no `code`, so branch on `status` and show `detail`. Insufficient scope is `401` on
|
|
224
|
+
`messages` and `otp` but `403` everywhere else, so branch on `status`, not `code`, there too.
|
|
225
|
+
|
|
226
|
+
The client retries `429`, `500`, `502`, `503`, `504`, and network errors or timeouts, up to
|
|
227
|
+
`max_retries` (default 2, so 3 attempts), only when the request is safe to repeat. `GET`, `PUT`,
|
|
228
|
+
`PATCH` and `DELETE` are always retried. A `POST` is retried only when it carries an
|
|
229
|
+
`Idempotency-Key`: every method that supports one generates a UUIDv4 per call (or uses your
|
|
230
|
+
`idempotency_key=`) and sends the same key on every retry, so the server replays instead of sending
|
|
231
|
+
twice. `messages.cancel`, `otp.verify`, `sender_ids.create`, `sender_ids.create_draft`,
|
|
232
|
+
`suppressions.create`, `suppressions.import_`, and any other 4xx are never retried. The client
|
|
233
|
+
honours `Retry-After` (seconds or an HTTP date); when that asks for more than 60s it raises instead,
|
|
234
|
+
with `retry_after` set. Otherwise it waits a random delay up to `min(8s, 0.5s * 2^(n-1))` (exponential
|
|
235
|
+
backoff with full jitter).
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
from opensms import OpensmsError
|
|
239
|
+
|
|
240
|
+
try:
|
|
241
|
+
client.messages.send(to="+254712345679", text="hi")
|
|
242
|
+
except OpensmsError as e:
|
|
243
|
+
print(e.status, e.detail)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Webhooks
|
|
247
|
+
|
|
248
|
+
Deliveries carry `X-OpenSMS-Signature: t=<unix>,v1=<hex>`, an HMAC-SHA256 of `"<t>.<raw body>"` keyed
|
|
249
|
+
with the endpoint's `whsec_...` secret used verbatim. Always verify the raw body before parsing it;
|
|
250
|
+
no API key is needed:
|
|
251
|
+
|
|
252
|
+
```python
|
|
253
|
+
from opensms import construct_event, verify_signature, OpensmsError
|
|
254
|
+
|
|
255
|
+
# e.g. in a Flask view
|
|
256
|
+
raw = request.get_data() # exact bytes received
|
|
257
|
+
header = request.headers.get("X-OpenSMS-Signature")
|
|
258
|
+
try:
|
|
259
|
+
event = construct_event(raw, header, secret) # secret = "whsec_..." from webhooks.create
|
|
260
|
+
except OpensmsError as e:
|
|
261
|
+
abort(400) # e.code: invalid_signature | expired_signature
|
|
262
|
+
print(event["type"], event["data"])
|
|
263
|
+
|
|
264
|
+
ok = verify_signature(raw, header, secret, tolerance_seconds=300) # boolean form
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The default tolerance is 300 seconds; both functions accept `now` to inject a fixed clock in tests.
|
|
268
|
+
|
|
269
|
+
## Testing
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
pip install -e ".[dev]"
|
|
273
|
+
pytest tests/test_unit.py tests/test_urllib_transport.py
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
The unit tests need no network. The live suite (`tests/test_live.py`, marked `live`) runs against a
|
|
277
|
+
sandbox when `OPENSMS_BASE_URL` and `OPENSMS_API_KEY` are set, and is skipped otherwise:
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
OPENSMS_BASE_URL=... OPENSMS_API_KEY=... pytest tests/test_live.py -v
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## License
|
|
284
|
+
|
|
285
|
+
MIT (c) OpenSMS
|
opensms-0.1.0/README.md
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# opensms (Python)
|
|
2
|
+
|
|
3
|
+
Official Python client for [opensms](https://opensms.io): prepaid SMS for Africa.
|
|
4
|
+
|
|
5
|
+
Pure standard library: no runtime dependencies. Python 3.8+. Fully typed (`py.typed`).
|
|
6
|
+
See the [monorepo README](https://github.com/opensms-io/opensms-sdks) for the other SDKs and
|
|
7
|
+
[spec/SURFACE.md](https://github.com/opensms-io/opensms-sdks/blob/main/spec/SURFACE.md) for the full API surface.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install opensms
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Not yet published to PyPI: until it is, install from this repo with `pip install ./packages/python` (run from the `sdks` directory), or `pip install -e ./packages/python` for an editable install.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from opensms import Opensms
|
|
21
|
+
|
|
22
|
+
client = Opensms(api_key="sk_test_...")
|
|
23
|
+
|
|
24
|
+
message = client.messages.send(to="+254712345678", text="Your order has shipped")
|
|
25
|
+
print(message["id"], message["status"], message["price"]) # price is a decimal string
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
A key starting `sk_test_` selects the sandbox, `sk_live_` the live environment; `client.environment`
|
|
29
|
+
reports which one is active, and a malformed key raises `ValueError` before any request is made.
|
|
30
|
+
Other constructor options: `base_url` (default `https://api.opensms.io`), `timeout` (seconds per
|
|
31
|
+
attempt, default 30), `max_retries` (default 2), and `transport` (swap the network layer for tests).
|
|
32
|
+
|
|
33
|
+
## More
|
|
34
|
+
|
|
35
|
+
One short example per resource, in the order they're listed on `client`.
|
|
36
|
+
|
|
37
|
+
### messages
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
m = client.messages.send(to="+254712345678", text="Hello", sender_id="ACME")
|
|
41
|
+
client.messages.get(m["id"])
|
|
42
|
+
client.messages.cancel(m["id"]) # only queued or scheduled messages
|
|
43
|
+
page = client.messages.list(limit=50, status="delivered", country="KE")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### batches
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
b = client.batches.create(items=[
|
|
50
|
+
{"to": "+254712345678", "text": "Hi Ada"},
|
|
51
|
+
])
|
|
52
|
+
client.batches.start(b["id"]) # batches are created "ready"
|
|
53
|
+
client.batches.get(b["id"])
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### otp
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
otp = client.otp.send(to="+254712345678", length=6, ttl_seconds=300)
|
|
60
|
+
result = client.otp.verify(otp_id=otp["otp_id"], code="123456")
|
|
61
|
+
print(result["valid"], result["attempts_left"])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### lookups
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
lk = client.lookups.create(to="+254712345678")
|
|
68
|
+
client.lookups.get(lk["id"])
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### contacts
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
ct = client.contacts.create(e164="+254712345678", name="Ada")
|
|
75
|
+
client.contacts.update(ct["id"], name="Ada L") # PATCH: other fields kept
|
|
76
|
+
client.contacts.list(limit=200)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### contact_groups
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
g = client.contact_groups.create(name="VIP", contact_ids=[ct["id"]])
|
|
83
|
+
client.contact_groups.send(g["id"], text="Sale starts today")
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### templates
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
tpl = client.templates.create(name="welcome", body="Hi {{name}}", traffic_type="transactional")
|
|
90
|
+
client.templates.update(tpl["id"], body="Hello {{name}}")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### webhooks
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
wh = client.webhooks.create(url="https://example.com/opensms", events=["message.delivered"])
|
|
97
|
+
secret = wh["secret"] # whsec_..., shown only once
|
|
98
|
+
client.webhooks.update(wh["id"], url=wh["url"], events=wh["events"], enabled=True) # full replacement
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### inbound
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
client.inbound.list()
|
|
105
|
+
client.inbound.reply(inbound_id, text="Thanks!") # live keys only
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### numbers
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
n = client.numbers.assign(country="KE", kind="long_code") # live keys only, charges the wallet
|
|
112
|
+
client.numbers.create_rule(n["id"], match="keyword", pattern="STOP", action="webhook", target="https://example.com/in")
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### sender_ids
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
draft = client.sender_ids.create_draft(source="application", value="ACME", kind="alphanumeric", countries=["KE"])
|
|
119
|
+
sid = client.sender_ids.create(value="ACME", kind="alphanumeric", countries=["KE"],
|
|
120
|
+
documents=[], use_case="transactional") # may charge fees; never auto-retried
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### suppressions
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
s = client.suppressions.create(e164="+254712345678", reason="manual")
|
|
127
|
+
client.suppressions.import_([{"e164": "+254712345679", "reason": "complaint"}])
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`import` is a Python keyword, so the method is `import_` (`getattr(client.suppressions, "import")` also works).
|
|
131
|
+
|
|
132
|
+
### compliance
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
client.compliance.list_countries()
|
|
136
|
+
client.compliance.get_country("KE")
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### wallet
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
client.wallet.balances()
|
|
143
|
+
client.wallet.ledger(limit=50) # not cursor based; page with before=<smallest id seen>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### pricing
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
client.pricing.get(product="sms", country="KE")
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### analytics
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
client.analytics.overview(range="7d")
|
|
156
|
+
client.analytics.by_country(from_="2026-09-01", to="2026-09-24") # from is a keyword, so from_
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### sandbox
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
client.sandbox.list_messages(limit=10) # rendered texts of sandbox sends, including OTP codes
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### countries
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
client.countries.list()
|
|
169
|
+
client.countries.carriers("KE")
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Pagination
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
for message in client.paginate(client.messages.list, limit=50):
|
|
176
|
+
print(message["id"])
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`client.paginate(list_method, *args, **params)` feeds each page's `next_cursor` back as `cursor` until
|
|
180
|
+
it is `None`. Bare-array endpoints and `wallet.ledger` are not paginated by it.
|
|
181
|
+
|
|
182
|
+
## Errors and retries
|
|
183
|
+
|
|
184
|
+
Every non-2xx response, and any network failure or timeout that survives all retries, raises
|
|
185
|
+
`OpensmsError`:
|
|
186
|
+
|
|
187
|
+
| Field | Notes |
|
|
188
|
+
|---|---|
|
|
189
|
+
| `status` | HTTP status; `0` means a network error, timeout, or webhook signature failure |
|
|
190
|
+
| `message` | `detail`, else `title`, else a generic message |
|
|
191
|
+
| `type` | problem `type` URI, usually `about:blank` |
|
|
192
|
+
| `title` | problem `title`, e.g. `"Bad Request"` |
|
|
193
|
+
| `detail` | the specific human readable reason |
|
|
194
|
+
| `code` | machine readable code; absent on most errors |
|
|
195
|
+
| `trace_id` | present when the server sets one |
|
|
196
|
+
| `errors` | field validation errors, `{field: [messages]}`, when present |
|
|
197
|
+
| `request_id` | `X-Request-ID`; set on message and OTP admission rejections |
|
|
198
|
+
| `retry_after` | seconds, from `Retry-After`, when the server sent it |
|
|
199
|
+
| `body` | raw decoded body (dict), or text when the body was not JSON |
|
|
200
|
+
|
|
201
|
+
Most errors carry no `code`, so branch on `status` and show `detail`. Insufficient scope is `401` on
|
|
202
|
+
`messages` and `otp` but `403` everywhere else, so branch on `status`, not `code`, there too.
|
|
203
|
+
|
|
204
|
+
The client retries `429`, `500`, `502`, `503`, `504`, and network errors or timeouts, up to
|
|
205
|
+
`max_retries` (default 2, so 3 attempts), only when the request is safe to repeat. `GET`, `PUT`,
|
|
206
|
+
`PATCH` and `DELETE` are always retried. A `POST` is retried only when it carries an
|
|
207
|
+
`Idempotency-Key`: every method that supports one generates a UUIDv4 per call (or uses your
|
|
208
|
+
`idempotency_key=`) and sends the same key on every retry, so the server replays instead of sending
|
|
209
|
+
twice. `messages.cancel`, `otp.verify`, `sender_ids.create`, `sender_ids.create_draft`,
|
|
210
|
+
`suppressions.create`, `suppressions.import_`, and any other 4xx are never retried. The client
|
|
211
|
+
honours `Retry-After` (seconds or an HTTP date); when that asks for more than 60s it raises instead,
|
|
212
|
+
with `retry_after` set. Otherwise it waits a random delay up to `min(8s, 0.5s * 2^(n-1))` (exponential
|
|
213
|
+
backoff with full jitter).
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
from opensms import OpensmsError
|
|
217
|
+
|
|
218
|
+
try:
|
|
219
|
+
client.messages.send(to="+254712345679", text="hi")
|
|
220
|
+
except OpensmsError as e:
|
|
221
|
+
print(e.status, e.detail)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Webhooks
|
|
225
|
+
|
|
226
|
+
Deliveries carry `X-OpenSMS-Signature: t=<unix>,v1=<hex>`, an HMAC-SHA256 of `"<t>.<raw body>"` keyed
|
|
227
|
+
with the endpoint's `whsec_...` secret used verbatim. Always verify the raw body before parsing it;
|
|
228
|
+
no API key is needed:
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
from opensms import construct_event, verify_signature, OpensmsError
|
|
232
|
+
|
|
233
|
+
# e.g. in a Flask view
|
|
234
|
+
raw = request.get_data() # exact bytes received
|
|
235
|
+
header = request.headers.get("X-OpenSMS-Signature")
|
|
236
|
+
try:
|
|
237
|
+
event = construct_event(raw, header, secret) # secret = "whsec_..." from webhooks.create
|
|
238
|
+
except OpensmsError as e:
|
|
239
|
+
abort(400) # e.code: invalid_signature | expired_signature
|
|
240
|
+
print(event["type"], event["data"])
|
|
241
|
+
|
|
242
|
+
ok = verify_signature(raw, header, secret, tolerance_seconds=300) # boolean form
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The default tolerance is 300 seconds; both functions accept `now` to inject a fixed clock in tests.
|
|
246
|
+
|
|
247
|
+
## Testing
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
pip install -e ".[dev]"
|
|
251
|
+
pytest tests/test_unit.py tests/test_urllib_transport.py
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
The unit tests need no network. The live suite (`tests/test_live.py`, marked `live`) runs against a
|
|
255
|
+
sandbox when `OPENSMS_BASE_URL` and `OPENSMS_API_KEY` are set, and is skipped otherwise:
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
OPENSMS_BASE_URL=... OPENSMS_API_KEY=... pytest tests/test_live.py -v
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT (c) OpenSMS
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "opensms"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official Python client for OpenSMS, the prepaid SMS API: send messages, OTPs, batches and lookups across Africa."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "OpenSMS" }]
|
|
13
|
+
keywords = ["sms", "otp", "opensms", "africa", "messaging", "api"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Communications :: Telephony",
|
|
20
|
+
"Typing :: Typed",
|
|
21
|
+
]
|
|
22
|
+
dependencies = []
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = ["pytest>=7"]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://opensms.io"
|
|
29
|
+
Repository = "https://github.com/opensms-io/opensms-sdks"
|
|
30
|
+
Issues = "https://github.com/opensms-io/opensms-sdks/issues"
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.wheel]
|
|
33
|
+
packages = ["src/opensms"]
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.sdist]
|
|
36
|
+
include = ["src/opensms", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
40
|
+
pythonpath = ["src"]
|
|
41
|
+
markers = ["live: integration tests against a real OpenSMS API (need OPENSMS_BASE_URL and OPENSMS_API_KEY)"]
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Official Python client for OpenSMS, the prepaid SMS API.
|
|
2
|
+
|
|
3
|
+
from opensms import Opensms
|
|
4
|
+
|
|
5
|
+
client = Opensms(api_key="sk_test_...")
|
|
6
|
+
client.messages.send(to="+254700000012", text="Your order has shipped")
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from ._transport import Transport, TransportRequest, TransportResponse, urllib_transport
|
|
10
|
+
from ._version import __version__
|
|
11
|
+
from .client import Opensms
|
|
12
|
+
from .errors import OpensmsError
|
|
13
|
+
from .models import Page
|
|
14
|
+
from .pagination import paginate
|
|
15
|
+
from .signature import SIGNATURE_HEADER, construct_event, verify_signature
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"Opensms",
|
|
19
|
+
"OpensmsError",
|
|
20
|
+
"Page",
|
|
21
|
+
"paginate",
|
|
22
|
+
"verify_signature",
|
|
23
|
+
"construct_event",
|
|
24
|
+
"SIGNATURE_HEADER",
|
|
25
|
+
"Transport",
|
|
26
|
+
"TransportRequest",
|
|
27
|
+
"TransportResponse",
|
|
28
|
+
"urllib_transport",
|
|
29
|
+
"__version__",
|
|
30
|
+
]
|