EmailVerify LogoEmailVerify

AI SDKs

Email checker integration with AI SDKs. Anthropic, OpenAI, LangChain, Vercel AI SDK guides.

使用流行的 AI 框架和 SDK 构建具有内置邮箱验证功能的 AI 应用。

支持的框架

对比

框架语言最适合复杂度
Vercel AI SDKTypeScriptNext.js 应用、流式传输
LangChainPython/JS复杂代理、链式调用
OpenAI Functions任意直接 GPT 集成
Anthropic Tools任意直接 Claude 集成

快速示例

这是一个适用于各框架的简单工具定义:

const verifyEmailTool = {
  name: 'verify_email',
  description: '验证邮箱地址是否有效且可接收邮件',
  parameters: {
    type: 'object',
    properties: {
      email: {
        type: 'string',
        description: '要验证的邮箱地址',
      },
    },
    required: ['email'],
  },
  execute: async ({ email }) => {
    const response = await fetch('https://api.emailverify.ai/v1/verify', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${process.env.EMAILVERIFY_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email }),
    });
    return response.json();
  },
};

最佳实践

1. 错误处理

始终在工具中优雅地处理 API 错误:

execute: async ({ email }) => {
  try {
    const response = await fetch('https://api.emailverify.ai/v1/verify', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${process.env.EMAILVERIFY_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email }),
    });

    if (!response.ok) {
      if (response.status === 429) {
        return { error: '超出速率限制。请稍后重试。' };
      }
      return { error: `验证失败: ${response.statusText}` };
    }

    return response.json();
  } catch (error) {
    return { error: '网络错误。无法访问验证服务。' };
  }
}

2. 清晰的工具描述

编写描述性的工具定义,让 AI 知道何时使用它们:

description: `验证邮箱地址是否有效且可接收邮件。

返回值:
- status: "valid"(有效)、"invalid"(无效)或 "unknown"(未知)
- deliverable: 邮箱是否可以接收邮件
- disposable: 是否为临时邮箱服务
- role: 是否为角色邮箱(如 info@ 或 support@)
- score: 置信度评分(0 到 1)

在以下情况下使用此工具:
- 用户要求验证邮箱
- 检查邮箱是否真实存在
- 验证联系信息`

3. 批量处理以提高效率

验证多个邮箱时,使用批量端点:

const verifyEmailsBulkTool = {
  name: 'verify_emails_bulk',
  description: '一次验证多个邮箱。比逐个验证更高效。',
  parameters: {
    type: 'object',
    properties: {
      emails: {
        type: 'array',
        items: { type: 'string' },
        maxItems: 100,
      },
    },
    required: ['emails'],
  },
};

4. 缓存结果

考虑缓存验证结果以节省积分:

const cache = new Map<string, { result: any; timestamp: number }>();
const CACHE_TTL = 3600000; // 1 小时

async function verifyWithCache(email: string) {
  const cached = cache.get(email);
  if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
    return cached.result;
  }

  const result = await verifyEmail(email);
  cache.set(email, { result, timestamp: Date.now() });
  return result;
}

下一步

On this page