Gmail Drafts API

A simple API that creates Gmail drafts via Google Apps Script. Perfect for automating outreach, notifications, or any workflow that needs to queue up emails for review before sending.

How It Works

Your App → Val Town HTTP Endpoint → Google Apps Script → Gmail Draft
  1. Val Town exposes an HTTP endpoint that accepts draft requests
  2. Google Apps Script runs in your Google account with Gmail permissions
  3. Drafts appear in your Gmail, ready for review and sending

Setup

Step 1: Deploy the Apps Script

  1. Go to script.google.com and create a new project
  2. Copy the contents of appscript.ts into the editor
  3. Click Deploy → New deployment
  4. Select Web app as the type
  5. Set "Execute as" to Me and "Who has access" to Anyone
  6. Click Deploy and copy the web app URL

Step 1b: Approve Permissions

Before the web app can create drafts, you need to authorize it to access your Gmail:

  1. In the Apps Script editor, find the testCreateDraft function
  2. Click the Run button (▶️) to execute it
  3. A dialog will appear asking you to authorize the app
  4. Click Review permissions → Select your Google account
  5. Click AdvancedGo to [project name] (unsafe)Allow
  6. The test will create a sample draft in your Gmail — check that it worked!

Note: You only need to do this once. After granting permissions, the web app will be able to create drafts automatically.

Step 2: Configure Val Town

  1. Fork this val to your account
  2. Add an environment variable:
    • Key: APPS_SCRIPT_URL
    • Value: Your Apps Script web app URL from Step 1

Step 3: Test It

curl -X POST https://YOUR-VAL-ENDPOINT/draft \ -H "Content-Type: application/json" \ -d '{ "recipient": "test@example.com", "subject": "Hello from the API", "body": "This is a <b>test draft</b>!" }'

API Reference

POST /draft

Create a new Gmail draft.

Request Body:

{ "recipient": "user@example.com", "subject": "Email subject", "body": "HTML body content with <b>formatting</b>", "cc": "optional@example.com" }

Response:

{ "status": "success", "message": "Draft created successfully", "draftId": "r1234567890" }

GET /

Health check endpoint.

Response:

{ "status": "Gmail Drafts API ready", "endpoints": ["POST /draft"] }

Email Body Tips

  • Use <br> for line breaks (not \n)
  • Full HTML is supported: <b>, <i>, <a href="">, <ul>, etc.
  • The plain text version is auto-generated by stripping HTML tags

Files

FilePurpose
main.tsHTTP endpoint (Val Town)
appscript.tsGoogle Apps Script code (copy to script.google.com)

Security Notes

  • The Apps Script runs with your Gmail permissions
  • Anyone with the Val Town endpoint URL can create drafts in your account
  • Consider adding authentication if exposing publicly
  • Drafts are only created—never sent automatically

License

MIT