Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
5 views 6 min read
Back to Blog
Tutorials

Temporary Email API in Python: Buy an Address and Read the Code

Temporary Email API in Python: Buy an Address and Read the Code

Python is the language most teams reach for when they need to script a verification flow, and the temporary email API in Python flow is short enough to fit in one file. Buy an address, poll the inbox, parse the code, release the address. The interesting parts are not the HTTP calls; they are the retry budget, the parsing pattern and the cleanup that runs even when something breaks. This guide covers all three with code you can adapt immediately.

Direct answer: To use the temporary email API in Python, send a POST to buy an address, poll the message endpoint with the returned identifier until a message appears, extract the code with a service-specific pattern, and send a release request when finished. Use the requests library and a timeout on every call.

Setting Up the Python Client

  1. Install the requests library if it is not already available.
  2. Read the API key and base URL from environment variables.
  3. Create a session object so headers and connection pooling are reused.
  4. Set a connect timeout and a read timeout on every request.
  5. Wrap the calls in a small class with one method per endpoint.

A session object matters more than it looks. It reuses TCP connections, which cuts latency during polling, and it gives you one place to set the authorization header. If you use async code, the same pattern applies with an async HTTP client.

Buying an Address With the Temporary Email API in Python

The buy call needs the server and the site or app you are verifying, and optionally a preferred domain. Send it as JSON and check the status code before parsing the body.

  • 200: the address is ready; read the address and the identifier.
  • 202: accepted and processing; poll the status endpoint.
  • 402: the wallet balance is insufficient; stop and top up.
  • 429: rate limited; back off before retrying.

Persist the identifier to a file or database before continuing. A script that buys an address and then crashes without saving the identifier has spent money for nothing.

Polling for the Message

Polling is a loop with three exit conditions: a message arrived, the budget expired, or the address is gone. Write it explicitly rather than using a fixed sleep and a single fetch.

  1. Set a maximum number of attempts and a delay between them.
  2. Fetch the message endpoint with the identifier.
  3. If the response is empty, sleep and try again.
  4. If a message is present, break and parse it.
  5. If the attempt count is exhausted, raise a clear exception with the identifier attached.

Two to three seconds between attempts is a reasonable default. Faster polling wastes requests and slower polling annoys users waiting on a screen.

Parsing the Code Reliably

The naive approach, a regex for four to eight digits, will match dates, order numbers and phone numbers in the message footer. Write a pattern per service, or anchor the search to the phrase the service uses, such as a verification phrase followed by digits. Test the pattern against a real message before you trust it.

Once extracted, hash the code if you need to compare later, and discard the plain value. Never print the code to stdout in a shared CI log, and never store it in a database column that is not designed for it.

Releasing the Address and Cleaning Up

The release call belongs in a finally block. Python makes this easy with a try/finally or a context manager. If the flow raises an exception halfway through, the release still runs, and your inventory does not leak.

Release is also a testing convenience. A released address cannot receive a stray message from a later run, so your assertions stay deterministic. Add the release to your test teardown and you will never wonder why a script passed twice and failed the third time.

Error Handling and Logging

SituationWhat to logWhat to do
Empty inbox after budgetIdentifier, attempt count, timestampsRaise a not-ready error and offer a reorder
402 responseIdentifier, endpoint, balanceTop up and retry once
404 responseIdentifier, timestampBuy a new address
Connection errorEndpoint, exception typeRetry with backoff if within budget

Log identifiers and outcomes, never codes. If you need to debug a parsing problem, log the message shape, not the extracted value.

Running It in Staging

Run the full script once against real inventory before you put it in a pipeline. Create an account at the SMS Feel registration page, copy the base URL and key from the SMS Feel developer documentation, and trigger a genuine message from a service you control.

Check the wallet balance first. A top-up mid-run turns a five-minute test into a debugging session, and a small balance is enough for a full cycle.

Frequently Asked Questions

Which Python library should I use for the API?

The requests library is the simplest choice for synchronous code. For async applications, the same pattern works with an async HTTP client and an async sleep.

How do I avoid duplicate purchases after a timeout?

Generate an idempotency key per purchase intent and reuse it across retries. Then a repeated request returns the original result instead of creating a second address.

Can I run this in a CI pipeline?

Yes, with a staging key, a small balance and assertions on the parsed code. Keep the key in a secret variable and release the address in teardown.

Why does the message endpoint return nothing for a long time?

Either the sending service is slow or the domain is filtered by the sender. Check the order status, then try a different domain before assuming the API is broken.

Is the same script usable for virtual numbers?

The overall pattern is identical, but the endpoints and fields differ. Read the documentation for the number flow before reusing the code.

Conclusion

A temporary email API in Python flow is four calls and a careful loop: buy, poll, parse, release. Put the release in a finally block, keep the parsing pattern specific to the service and log identifiers rather than codes. To run it against real inventory, create a free SMS Feel account and start with the API documentation.

Related reading: more Python integration guides on the SMS Feel blog.

Tags

#temporary email api in python #python temporary email #temporary email api #polling #otp code #api #docs
Contact us