Temporary Email API in Python: Buy an Address and Read the Code
If you want your program to sign up on a website and read the confirmation code without you, you need an address that your code can buy and read. The OtpGet email API does that with plain HTTP calls. This example uses Python and the requests library, and covers Server 2 where you choose the domain and the website.
What you need
- An OtpGet account with a small balance.
- Your API key from the dashboard. Keep it out of public code.
- Python 3 and
pip install requests.
The flow
getEmailServiceson server 2 for your site, to see domains and prices.buyEmailwith the domain you chose, to get an address and anemail_id.- Use the address on the website.
getEmailCodeevery 30 to 35 seconds until the status is 200.cancelEmailif no code arrives, to get the balance back.
The code
import time, requests
BASE = "https://otpget.com/stubs/email-handler.php"
KEY = "YOUR_API_KEY"
SITE = "instagram.com"
def call(**params):
params["api_key"] = KEY
r = requests.get(BASE, params=params, timeout=30)
return r.json() # status is a string: "200", "202", "402" ...
# 1. which domains can I use for this site, and what do they cost?
lst = call(action="getEmailServices", server=2, site=SITE)
if lst["status"] != "200":
raise SystemExit(lst)
cheapest = min(lst["domains"], key=lambda d: d["price"])
# 2. buy the address
order = call(action="buyEmail", server=2, domain=cheapest["domain"], site=SITE)
if order["status"] != "200":
raise SystemExit(order)
print("use this address:", order["email"])
# 3. poll for the code (do the sign-up on the website now)
email_id = order["email_id"]
deadline = time.time() + 25 * 60
while time.time() < deadline:
time.sleep(32)
res = call(action="getEmailCode", email_id=email_id)
if res["status"] == "200":
print("code:", res["messages"][0]["code"])
break
if res["status"] == "300":
print("order was cancelled")
break
else:
print(call(action="cancelEmail", email_id=email_id)) # refundThings this example does on purpose
- It checks
statusas a string, because every reply is HTTP 200 and the real result is in the body. - It sends the same
sitein the list and the buy call. - It stops at 25 minutes and cancels, because unfilled orders are cancelled and refunded after 30 minutes anyway.
- It waits about 32 seconds between polls instead of hammering the API.
Before you go live
- Add your own spending limit.
- Handle 402 (low balance), 404 (no stock) and 500 replies.
- Never print or commit your key.
- Test with a small balance first.
Making the script safer
- Wrap the buy call in a function that refuses to run if the balance is below a set amount.
- Keep a list of domains in your preferred order and try the next one when the API returns 404.
- Store the order id in a file or a database before you start polling, so a crash does not lose it.
- Add a hard limit on how many addresses the script may buy per hour.
Reading the code from the reply
The code arrives inside messages. The reply can contain the whole text of the email, so if a website sends a link instead of a code, you may need to read the link from the message text. Print the full reply once during testing, and then write the small piece of code that picks what you need.
Questions people ask
Can I run it on Server 1?
Yes, with different parameters: pick a domain with getEmailDomains, then buy with a domain and a service_id such as ig or tg.
How often should I poll?
About every 30 to 35 seconds. Faster polling does not make the email arrive sooner.
Related guides and pages
Create a free OtpGet account and try a temporary email with a small balance.