React Examples

React component examples for integrating Metigan. Note: API calls should typically be made from server-side code or API routes, not directly from React components.

React Hook for Email Sending

useEmail.tsTypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useState } from 'react';

interface UseEmailReturn {
  sendEmail: (params: EmailParams) => Promise<void>;
  loading: boolean;
  error: string | null;
  success: boolean;
}

export function useEmail(): UseEmailReturn {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [success, setSuccess] = useState(false);

  const sendEmail = async (params: EmailParams) => {
    setLoading(true);
    setError(null);
    setSuccess(false);

    try {
      // Call your API route, not Metigan directly from client
      const response = await fetch('/api/send-email', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(params)
      });

      const data = await response.json();

      if (data.success) {
        setSuccess(true);
      } else {
        setError(data.message || 'Failed to send email');
      }
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Unknown error');
    } finally {
      setLoading(false);
    }
  };

  return { sendEmail, loading, error, success };
}
Security

Never use API keys in client-side React code. Always make API calls through server-side API routes or server components to keep your API keys secure.