Skip to content

Commit d68e682

Browse files
docs(readme): Update usage examples and address the issue #40
1 parent 11865f3 commit d68e682

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,9 @@ The Mailgun API is part of the Sinch family and enables you to send, track, and
210210
### Base URL
211211

212212
All API calls referenced in our documentation start with a base URL. Mailgun allows the ability to send and receive
213-
email in both US and EU regions. Be sure to use the appropriate base URL based on which region you have created for your
214-
domain.
213+
email in both US and EU regions.
215214

216-
It is also important to note that Mailgun uses URI versioning for our API endpoints, and some endpoints may have
217-
different versions than others. Please reference the version stated in the URL for each endpoint.
215+
If you are using a proxy or a regional endpoint (such as the EU infrastructure), you can configure a custom `api_url` during initialization.
218216

219217
For domains created in our US region the base URL is:
220218

@@ -228,11 +226,16 @@ For domains created in our EU region the base URL is:
228226
https://api.eu.mailgun.net/
229227
```
230228

231-
Your Mailgun account may contain multiple sending domains. To avoid passing the domain name as a query parameter, most
232-
API URLs must include the name of the domain you are interested in:
229+
**⚠️ Important:** The `api_url` parameter must strictly be the **base host only** (e.g., `https://api.eu.mailgun.net`). Do **not** append API version paths (like `/v3` or `/v4`) to this string. The SDK's data-driven routing engine automatically appends the correct, endpoint-specific API version under the hood.
233230

234-
```sh
235-
https://api.mailgun.net/v3/mydomain.com
231+
```python
232+
import os
233+
from mailgun.client import Client
234+
235+
# Pass ONLY the base domain
236+
with Client(auth=("api", os.environ["APIKEY"]), api_url="https://api.eu.mailgun.net") as client:
237+
# do someshings
238+
pass
236239
```
237240

238241
### Authentication
@@ -266,26 +269,21 @@ Synchronous vs Asynchronous Client.
266269

267270
### Client
268271

269-
Initialize your [Mailgun](http://www.mailgun.com/) client:
270-
271-
```python
272-
from mailgun.client import Client
273-
import os
274-
275-
auth = ("api", os.environ["APIKEY"])
276-
client = Client(auth=auth)
277-
```
278-
279272
#### Client Lifecycle & Resource Management
280273

274+
Initialize your [Mailgun](http://www.mailgun.com/) client.
275+
281276
> [!TIP]
282277
> **New in v1.7.0:** The SDK now utilizes connection pooling (`requests.Session`) under the hood to dramatically improve performance by reusing TLS connections.
283278
284279
**The Simple Variant (Backward Compatible)**
285280
For simple scripts, lambdas, or single-request apps, you can initialize and use the client directly. Python's garbage collector will eventually clean up the connection.
286281

287282
```python
288-
client = Client(auth=("api", "KEY"))
283+
import os
284+
from mailgun.client import Client
285+
286+
client = Client(auth=("api", os.environ["APIKEY"]))
289287
client.messages.create(data={"to": "user@example.com"})
290288
```
291289

@@ -298,8 +296,11 @@ If you are running long-lived applications (like Celery workers, web servers, or
298296
For production applications, \**always use the client as a Context Manager* (`with`) or explicitly call `client.close()`. This ensures deterministic release of TCP connection pools.
299297

300298
```python
299+
import os
300+
from mailgun.client import Client
301+
301302
# Sockets are safely managed and closed automatically
302-
with Client(auth=("api", "KEY")) as client:
303+
with Client(auth=("api", os.environ["APIKEY"])) as client:
303304
client.messages.create(data={"to": "user@example.com"})
304305
```
305306

@@ -308,7 +309,7 @@ with Client(auth=("api", "KEY")) as client:
308309
By default, the SDK routes traffic to the US servers (`https://api.mailgun.net`). If you are operating in the EU, you can override the base URL during initialization:
309310

310311
```python
311-
client = Client(auth=("api", "KEY"), api_url="https://api.eu.mailgun.net")
312+
client = Client(auth=("api", os.environ["APIKEY"]), api_url="https://api.eu.mailgun.net")
312313
```
313314

314315
The SDK also implements Timeouts by default `read=60.0s` (but can take a tuple with connect/read `(10.0, 60.0)` to ensure your application fails-fast during network partitions but remains patient while Mailgun processes heavy analytical queries).
@@ -322,8 +323,6 @@ import asyncio
322323
import os
323324
from mailgun.client import AsyncClient
324325

325-
auth = ("api", os.environ["APIKEY"])
326-
327326

328327
async def main():
329328
# BEST PRACTICE: Use the async context manager for safe connection pooling

0 commit comments

Comments
 (0)