सामग्री पर जाएँ
उपयोग के तरीके

टेम्प मेल API: API key, अनुमतियाँ और अपने ऐप की टेस्टिंग

Temp mail API key से अनुमति वाले इनबॉक्स और मैसेज पढ़ें। सर्वर पर key रखें, सीमित अनुमति दें और Node.js उदाहरण से टेस्ट करें।

इस पृष्ठ पर

Temp mail API key से आप अपने अकाउंट की उपलब्ध सुविधाओं तक प्रोग्राम से पहुँचते हैं। Key प्लान की पात्रता की जगह नहीं लेती और हर पता नहीं खोलती; इनबॉक्स सिर्फ़ ईमेल रिसीव करते हैं।

उदाहरण: टेम्प मेल API से टेस्ट मैसेज पढ़ना

अलग से बनाई API key सिर्फ़ अनुमति वाले इनबॉक्स और कामों के लिए इस्तेमाल करें। उसे उदाहरणों और लॉग से दूर रखें और बिना संवेदनशील डेटा वाले मैसेज से टेस्ट करें।

API key को सीमित अनुमति के साथ सुरक्षित रखें

अकाउंट में सिर्फ़ ज़रूरी अनुमति वाली key बनाएँ। उसे सर्वर पर रखें, पब्लिक JavaScript, URL या रिपॉज़िटरी में नहीं। endpoints, एरर और लिमिट के लिए मौजूदा दस्तावेज़ देखें।

क्लाइंट में ईमेल डेटा को सावधानी से संभालें

नकली डेटा से इनबॉक्स बनाना, अनुमति वाला पढ़ना और एरर जाँचें। इनबॉक्स की अवधि और मैसेज रखने के नियम API में भी लागू हैं। बेकार key तय विकल्प से रद्द करें; अनुमति न मिलने पर उसे दरकिनार करने की कोशिश न करें।

क्या एक API key से हर इनबॉक्स खुल जाता है?

नहीं। अकाउंट, key का दायरा और इनबॉक्स की अनुमति तय करते हैं।

क्या API key को frontend कोड में रख सकते हैं?

नहीं। key को खुले कोड और लॉग से दूर रखें।

API से ईमेल रिसीव करने का व्यावहारिक उदाहरण

पात्र इनबॉक्स और न्यूनतम अनुमतियों वाली कुंजी तैयार करें। नीचे दिया सर्वर क्लाइंट प्रकाशित मार्गों का उपयोग करता है; कुंजी को ब्राउज़र या सार्वजनिक लॉग में न रखें।

// Receive-only example. Keep this module and its token on the Node/server side.
import {setTimeout as sleep} from 'node:timers/promises';

export function createEmailClient({token, base='https://tempmailace.com', intervalMs=2000}) {
  const origin=new URL(base);
  const local=['127.0.0.1','localhost','[::1]'].includes(origin.hostname);
  if (origin.username || origin.password || origin.pathname!=='/' || origin.search || origin.hash ||
      !(origin.origin==='https://tempmailace.com' || (local && origin.protocol==='http:'))) {
    throw new Error('Use the verified production origin or a local contract-test server');
  }
  if (!token) throw new Error('A scoped server-side API token is required');
  const segment=x=>encodeURIComponent(String(x));
  async function get(path, deadline) {
    while(Date.now()<deadline) {
      const response=await fetch(origin.origin+path, {
        headers:{Authorization:`Bearer ${token}`,Accept:'application/json'},
        redirect:'error',signal:AbortSignal.timeout(Math.max(1,Math.min(10000,deadline-Date.now())))
      });
      if (response.status===429) {
        const retry=response.headers.get('retry-after');
        const seconds=retry===null ? NaN : Number(retry);
        const waitMs=Number.isFinite(seconds) ? Math.max(0,seconds*1000) : Math.max(0,Date.parse(retry||'')-Date.now());
        const pause=Math.max(intervalMs,Number.isFinite(waitMs)?waitMs:intervalMs);
        await response.body?.cancel();
        if (Date.now()+pause>=deadline) break;
        await sleep(pause);continue;
      }
      if(!response.ok) {await response.body?.cancel();throw new Error(`Email API HTTP ${response.status}`);}
      return response.json();
    }
    throw new Error('Email wait timed out');
  }
  async function scan(inboxId,deadline) {
    const messages=[];let offset=0;const offsets=new Set();
    for(let page=0;page<100;page++) {
      if(offsets.has(offset)) throw new Error('Repeated pagination offset');
      offsets.add(offset);
      const payload=await get(`/api/developer/v1/inboxes/${segment(inboxId)}/messages?offset=${offset}`,deadline);
      if(!Array.isArray(payload.items) || typeof payload.has_more!=='boolean') throw new Error('Unexpected messages contract');
      messages.push(...payload.items);
      if(!payload.has_more) return messages;
      if(!Number.isInteger(payload.next_offset) || payload.next_offset<=offset) throw new Error('Invalid pagination offset');
      offset=payload.next_offset;
    }
    throw new Error('Pagination budget exceeded; dedicate an inbox to this test');
  }
  return {
    listInboxes:({timeoutMs=15000}={})=>get('/api/developer/v1/inboxes',Date.now()+timeoutMs),
    async waitForMessage({inboxId,subject,sender,since,timeoutMs=60000,excludeIds=[]}) {
      if(!inboxId || !subject || !sender || !Number.isFinite(Date.parse(since))) throw new Error('inboxId, exact subject, exact sender and ISO since are required');
      const deadline=Date.now()+timeoutMs,excluded=new Set(excludeIds),after=Date.parse(since);
      while(Date.now()<deadline) {
        const items=await scan(inboxId,deadline);
        const match=items.find(m=>m.id && !excluded.has(m.id) && m.subject===subject && m.from===sender && Date.parse(m.received_at)>=after);
        if(match) {
          const full=await get(`/api/developer/v1/inboxes/${segment(inboxId)}/messages/${segment(match.id)}`,deadline);
          if(full.id!==match.id || full.subject!==subject || full.from!==sender) throw new Error('Message changed during read');
          return full;
        }
        const remaining=deadline-Date.now();if(remaining<=0)break;
        await sleep(Math.min(intervalMs,remaining));
      }
      throw new Error('Email wait timed out');
    }
  };
}

// This is the exact format of the owned demo application, not a universal OTP parser.
export function demoCode(message) {
  const match=String(message.text||'').match(/Verification code:\s*(\d{6})(?!\d)/);
  if(!match) throw new Error('The demo six-digit code was not found');
  return match[1];
}

// Validate a link before a browser test follows it. Never navigate to arbitrary mail HTML.
export function demoVerificationLink(message, appOrigin) {
  const raw=String(message.text||'').match(/https?:\/\/[^\s<>"']+/)?.[0];
  if(!raw)throw new Error('No demo link');
  const link=new URL(raw),app=new URL(appOrigin);
  if(link.origin!==app.origin || link.pathname!=='/verify-email' || !link.searchParams.get('token') || link.username || link.password)throw new Error('Untrusted verification link');
  return link.href;
}

API क्लाइंट चलाना और नतीजे जाँचना

सर्वर टेस्ट से क्लाइंट चलाएँ। सीमित समय तक अपेक्षित संदेश की प्रतीक्षा करें और प्रेषक व सामग्री जाँचें। अनुमति की त्रुटि या इनबॉक्स की अवधि समाप्त होना, डिलीवरी में देरी से अलग स्थिति है।

पहला ब्लॉक Node.js सर्वर पर email-client.mjs नाम से रखें, जहाँ fetch और AbortSignal.timeout उपलब्ध हों। सर्वर या CI सेटिंग में TMA_API_KEY, TMA_INBOX_ID, TEST_SUBJECT, TEST_SENDER और TEST_SINCE दें: कुंजी, इनबॉक्स ID, अनूठा विषय, सटीक प्रेषक मान और भेजने से पहले का ISO समय। प्रतीक्षा शुरू करने से पहले अपने ऐप से टेस्ट संदेश भेजें; यह API उदाहरण संदेश प्राप्त करता है, भेजता नहीं।

import {createEmailClient} from './email-client.mjs';

const client = createEmailClient({token: process.env.TMA_API_KEY});
await client.waitForMessage({
  inboxId: process.env.TMA_INBOX_ID,
  subject: process.env.TEST_SUBJECT,
  sender: process.env.TEST_SENDER,
  since: process.env.TEST_SINCE,
  timeoutMs: 60000
});
console.log('The matching test message was read.');

टेम्प मेल API पर और गाइड

अधिक जानकारी के लिए देखें:

क्या इस विषय में मदद चाहिए?

सही टीम से संपर्क करें या सभी आधिकारिक संपर्क माध्यम देखें।