åæ
ãã®èšäºã§ã¯ä»¥äžãåæãšããŠããŸãã
- Supabase ã¢ã«ãŠã³ããäœææžã¿ã§ããããš
- Supabase CLI ãã€ã³ã¹ããŒã«æžã¿ã§ããããš
- Resend ã¢ã«ãŠã³ããäœææžã¿ã§ããããš
- Resend ã§ç¬èªãã¡ã€ã³ã®èšå®ãå®äºããŠããããšããã®èšäºã§ã¯
example.com
ã Resend ã«ç»é²æžã¿ã§ãããšããŸãã curl
ã³ãã³ãã䜿çšããŸã
ãããŸã Resend ã®ã¢ã«ãŠã³ãäœæãç¬èªãã¡ã€ã³ã®èšå®ãå®äºããŠããªãå Žåã¯ããããã°ä»¥äžã®èšäºãåèã«ããŠã¿ãŠãã ããã
âïž Resendã§ç¬èªãã¡ã€ã³ã䜿ãããã®èšå®æé
Resendã§ç¬èªãã¡ã€ã³ããã®ã¡ãŒã«ãéä¿¡ããããã«ç¬èªãã¡ã€ã³ãèšå®ããæé ã解説ããŸãã
ritaiz.com
ãã®èšäºã®ãŽãŒã«
ãã®èšäºã§ã¯ã以äžããŽãŒã«ãšããŸãã
- POST ãªã¯ãšã¹ããåã㊠Resend ã䜿ã£ãŠã¡ãŒã«ãéä¿¡ãã Supabase Edge Function 㧠äœæããããã€ãã
curl
ã³ãã³ãã䜿ã£ãŠ Supabse Edge Function ãå®è¡ããåäœç¢ºèªãã
ãã®èšäºã¯ä»¥äžã® Supabase å ¬åŒããã¥ã¡ã³ããåèã«ããŸããã
Supabase Edge Function ãäœæãã
ããã§ã¯ãmy-project
ãšãããããžã§ã¯ãçšã®ãã£ã¬ã¯ããªãããããã®äžã« Supabase Edge Function ãäœããŸãã
以äžã®ããã«supabase functions new
ã³ãã³ããå®è¡ããŸãã
ããã§ã¯ãresend
ãšããååã® Supabase Edge Function ãäœæããŸãã
$ cd ~/my-project
$ supabase functions new resend
Created new Function at supabase/functions/resend
supabase functions new
ã«ãã£ãŠsupabase/functions
ã«resend
ãäœæãããŸãã
å
容ã¯ä»¥äžã®ããã«index.ts
ãããã®ã¿ã§ããããã« Resend ã䜿ã£ãã¡ãŒã«éä¿¡åŠç ã®å
容ãèšè¿°ããŠãããŸãã
.
âââ .vscode
â âââ extensions.json
â âââ settings.json
âââ resend
âââ index.ts
Resend ã䜿ã£ãã¡ãŒã«éä¿¡åŠç
以äžã®ããã«ãSupabase Edge Function 㧠Resend ã䜿ã£ãã¡ãŒã«éä¿¡åŠçãå®è£ ããŸãã
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
// Supabaseã«ã»ããããã·ãŒã¯ã¬ããã䜿çšãã
// ã·ãŒã¯ã¬ããã®èšå®æ¹æ³ã¯åŸè¿°
const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY');
const handler = async (_request: Request): Promise<Response> => {
// _requestããéä¿¡å
ã¡ãŒã«ã¢ãã¬ã¹ãšéä¿¡å
容ãååŸãã
const _requestBody = await _request.json();
// ãã®Supabase Edge Functionã§ã¯ä»¥äžã®jsonãåãåãããšãæ³å®ããŠãã
// {
// body: {
// to: string,
// message: string,
// subject: string,
// },
// }
const res = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${RESEND_API_KEY}`,
},
body: JSON.stringify({
from: 'resend edge function <[email protected]>',
to: [_requestBody.to],
subject: _requestBody.subject,
html: `<h1>以äžã®ãåãåãããåãä»ããŸããã</h1></br>${_requestBody.message}`,
}),
});
const data = await res.json();
return new Response(JSON.stringify(data), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
};
serve(handler);
äžèšã® Supabase Edge Function ã§ã¯ã以äžã®ãã©ãŒãããã®ããŒã¿ãåãåãããšãæ³å®ããŠããŸãã
{
"body": {
"to": "éä¿¡å
ã¡ãŒã«ã¢ãã¬ã¹",
"message": "éä¿¡å
容",
"subject": "件å"
}
}
Supabase Edge Function ã POST ãªã¯ãšã¹ããéããŠåãåã£ãããŒã¿ãå
ã«ãResend ã® API ã䜿ã£ãŠã¡ãŒã«ãéä¿¡ããŸãã
ãªããã¡ãŒã«ã®éä¿¡å
ã¯from
ã®éšåã§äŸãšããŠresend edge function <[email protected]>
ãæå®ããŠããŸããã
ããã§æå®ã§ããã¡ãŒã«ã¢ãã¬ã¹ã®ãã¡ã€ã³ã¯ Resend ã«ç»é²å®äºããŠããç¬èªãã¡ã€ã³ã®ã¿ã«ãªããŸãããã以å€ã®ç¬èªãã¡ã€ã³ãæå®ããå Žåã¯ãšã©ãŒãšãªããŸãã
Supabase Edge Function çšã®ã·ãŒã¯ã¬ãããèšå®ãã
åç¯ã®ã³ãŒãã®äžã§ã¯ãconst RESEND_API_KEY = Deno.env.get("RESEND_API_KEY");
ã®éšå㧠Resend çšã® API ããŒã Supabase Edge Function ã®ã·ãŒã¯ã¬ããããååŸããŠäœ¿çšããŠããŸãã
Supabase ã® Edge Function ã®äžã§äœ¿çšããã·ãŒã¯ã¬ããïŒãŸãã¯ç°å¢å€æ°ïŒã¯ãSupabase CLI ã®supabase secrets set
ã³ãã³ãã䜿çšããŠèšå®ããŸãã
å
·äœçã«ã¯ã以äžãå®è¡ããããšã§ã·ãŒã¯ã¬ãããèšå®ã§ããŸãã
$ supabase secrets set RESEND_API_KEY=re_123456789
supabase secrets set
ã³ãã³ãã¯ä»¥äžã®æ§æã§äœ¿çšããŸãã
$ supabase secrets set ç°å¢å€æ°å=å€
ç°å¢å€æ°ã®èšå®ãæåãããšä»¥äžã®ãããªã¬ã¹ãã³ã¹ãè¿ã£ãŠããŸãã
$ supabase secrets set RESEND_API_KEY=re_123456789
Finished supabase secrets set.
ããäžèšãå®è¡ããæã«Enter your project ref:
ãšè¡šç€ºãããå Žåã¯ãèªèº«ã®ãããžã§ã¯ãã® Reference ID ãå
¥åããå¿
èŠããããŸãã
supabase secrets list
ãå®è¡ããŠèšå®æžã¿ã®ã·ãŒã¯ã¬ããã確èªããããšãã§ããŸãã
$ supabase secrets list
NAME â DIGEST
ââââââââââââââââââŒâââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
RESEND_API_KEY â 2c91d2a82x1pa8bba6bc4b438db52811486794916fb1ea14da7d051dd28172d0
ãããã€ãã
以äžãå®è¡ããããšã§ Supabase Edge Function ããããã€ããŸãã
$ supabase functions deploy resend --no-verify-jwt
以äžãå®è¡çµæã§ãã
$ supabase functions deploy resend --no-verify-jwt
Version 1.30.3 is already installed
Bundling resend
Deploying resend (script size: 21.26kB)
Deployed Function resend on project xxxxxxxxxx
You can inspect your deployment in the Dashboard: https://supabase.com/dashboard/project/xxxxxxxxxx/functions/resend/details
ãããã€ãæ£åžžã«å®äºãããšã以äžã®ããã« Supabase ã®ç®¡çç»é¢ã® Edge Function ã®äžèŠ§ã«ãããã€ãã Edge Function ã衚瀺ãããŸãã
äžèšã«è¡šç€ºãããŠããããã«ããããã€ãã Supabase Edge Function ã® URL ã«å¯Ÿã㊠POST ãªã¯ãšã¹ããéãããšã§å®è¡ããŸãã
--no-verify-jwtã«ã€ããŠ
ãªãã·ã§ã³ã§æå®ããŠãã--no-verify-jwt
ã¯ããã®Supabase Edge
Functionãå€éšããåŒã³åºããããã«ããããã®ãªãã·ã§ã³ã§ãã
ããªãã¡--no-verify-jwt
ãä»äžããŠãããã€ããSupabase Edge
Functionã¯ããã®ãããã€å
ã®URLãããããã°èª°ã§ãåŒã³ã ããŸãã
ãããã£ãŠããã®ããšãèžãŸããŠä»äžããå¿
èŠããããŸãããã誰ã§ãåŒã³åºããŠããŸããšåé¡ãããå Žåã¯ãSupabase
Edge
Functionã®äžã§åŒã³åºãå
ã®ãã§ãã¯ããããããããã¯--no-verify-jwt
ãä»äžããã«Supabaseã®ããªã¬ã䜿ã£ãããªã¯ãšã¹ãã®äžã«JWTãå«ããŠèªèšŒãããªã©ãå¿
èŠã§ãã
åäœç¢ºèªãã
åäœç¢ºèªãšããŠãããã€ãã Supabase Edge Function ã«curl
ã³ãã³ãã䜿ã£ãŠ POST ãªã¯ãšã¹ããéä¿¡ããŸãã
äŸãã°ã以äžãå®è¡ãããšãéä¿¡ããå
容ã«å¿ããã¡ãŒã«ã Resend ã§éä¿¡ã§ããŸãã
$ curl -X POST 'https://xxxxxxxxxxxxxxx.supabase.co/functions/v1/resend' \
-d $'{
"to": "[email protected]",
"subject": "Hello World from Supabase Edge Function",
"message": "ã¡ãŒã«éä¿¡ãã¹ã"
}'
以äžãå®è¡çµæã§ããéä¿¡ããã¡ãŒã«ã® Resend ã® ID ãè¿ã£ãŠããŸãã
$ curl -X POST 'https://xxxxxxxxxxxxxxx.supabase.co/functions/v1/resend' \
-d $'{
"to": "[email protected]",
"subject": "Hello World from Supabase Edge Function",
"message": "ã¡ãŒã«éä¿¡ãã¹ã"
}'
{"id":"3b00111a-2918-9312-kj3e-888cc3c3729f"}%
ãã Supabase Edge Function ã®äžã§from
ã«æå®ããã¡ãŒã«ã¢ãã¬ã¹ã®ãã¡ã€ã³ã Resend ã«ç»é²ãããŠããªãå Žåã¯ã以äžã®ãããªã¬ã¹ãã³ã¹ãè¿ã£ãŠããŸãã
$ curl -X POST 'https://xxxxxxxxxxxxxxx.supabase.co/functions/v1/resend' \
-d $'{
"to": "[email protected]",
"subject": "Hello World from Supabase Edge Function",
"message": "ã¡ãŒã«éä¿¡ãã¹ã"
}'
{"statusCode":403,"message":"The example.com domain is not verified. Please, add and verify your domain on https://resend.com/domains","name":"validation_error"}%
--no-verify-jwt ãä»äžããªãå Žå
以äžã®ããã«--no-verify-jwt
ãä»äžããã«ãããã€ããå Žåã¯ã
$ supabase functions deploy resend
以äžã®ããã«ãªã¯ãšã¹ãå
ã§Authorization
ããããŒã« Supabase ã® JWT ãå«ããããšã§ Supabase Edge Function ãå®è¡ã§ããŸãã
$ curl -X POST 'https://xxxxxxxxxxxxxxx.supabase.co/functions/v1/resend' \
-H 'Authorization: Bearer eyJhb**********************' \
-H 'Content-Type: application/json' \
-d $'{
"to": "[email protected]",
"subject": "Hello World from Supabase Edge Function with Auth",
"message": "èªèšŒã¡ãŒã«éä¿¡ãã¹ã"
}'
JWT ããªãå Žåã¯Missing authorization header
ãééã£ãŠããå Žåã¯Invalid JWT
ãšãããšã©ãŒãè¿ã£ãŠããŸãã
ããã§æå®ãã JWT ã¯ãSupabase ã®ãããžã§ã¯ãã®èšå®ç»é¢ïŒãµã€ãã¡ãã¥ãŒã®æ¯è»ããŒã¯ããã¢ã¯ã»ã¹ïŒå
ã®API
ã¿ãããŒãžãProject API keys
ã®äžã«ããanon public
ããŒã䜿çšã§ããŸãã
ãŸãšã
Supabase Edge Function 㧠Resend ã䜿ã£ãŠã¡ãŒã«éä¿¡åŠçãå®è£
ããæé ã解説ããŸããã
Supabase Edge Function ã䜿ãããšã§ HTTP ãªã¯ãšã¹ãçµç±ã§ã¡ãŒã«éä¿¡åŠçãå®è¡ããããä»ã«ã Supabase å
ã®ããŒã¿ããŒã¹ã«æ°ããããŒã¿ãè¿œå ãããããæŽæ°ããããããããšãããªã¬ã«å®è¡ãã§ããŸãã