Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Send Email with SparkPost and Node.js on Google App Engine Flexible Environment description: Learn how to send email via SparkPost from a Node.js app to Google App Engine flexible environment. author: ewandennis tags: App Engine, SparkPost, Express.js, Node.js date_published: 2017-03-22


SparkPost—Email delivery services built for developers, by developers.

– sparkpost.com

You can check out Node.js and Google Cloud Platform to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud Platform.


  1. Create a project in the Google Cloud Platform Console. Remember to take note of your project ID; you'll need it later to access your app at http://YOUR_PROJECT_ID.appspot.com
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.


  1. Sign up for a SparkPost account.

  2. Create an API key with the "Transmissions: Read/Write" privilege.

  3. Initialize a package.json file with the following command:

    npm init
  4. Install some dependencies, including the SparkPost Node.js client:

    npm install --save express body-parser pug sparkpost


  1. Create a server.js file with the following contents:

    'use strict';
    const http = require('http');
    const express = require('express');
    const path = require('path');
    const bodyParser = require('body-parser');
    const SparkPost = require('sparkpost');
    const spClient = new SparkPost(process.env.SPARKPOST_API_KEY);
    const app = express();
    const srv = http.Server(app);
    // Setup view engine
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'pug');
    // Parse form data
    app.use(bodyParser.urlencoded({ extended: false }));
    app.get('/', (req, res) => res.render('index'));
    app.post('/hello', (req, res, next) => {
        options: { sandbox: true },
        content: {
          from: 'appengine-node-demo@sparkpostbox.com',
          subject: 'Hello from Google AppEngine!',
          text: 'Google AppEngine + Node.js + SparkPost = awesome!'
        recipients: [
          {address: req.body.email} 
      }).then(result => {
        res.render('index', {sent: true});
      }).catch(err => {
        res.render('index', {err: err});
    srv.listen(process.env.PORT || 8080, () => {
      console.log(`Listening on ${srv.address().port}`);
  2. Create a directory named views:

    mkdir views
  3. Create a file named index.pug inside the views directory with the following contents:

    doctype html
        title= title
        h1 hello world!
        p express.js + sparkpost on google app engine.
        if sent
          p email sent!
        if err
          p Oh my. Something's not right:
              each e in err.errors
                  strong=e.message+': '
          form(name="hello", action="/hello", method="post")
            input(type="email", placeholder="enter your email to send yourself a hello world message", name="email", style="width: 50%; margin-right: 15px;")
            input(type="submit", value="send")


  1. Run the app with the following command:

    SPARKPOST_API_KEY=your-sparkpost-api-key npm start
  2. Visit http://localhost:8080 to try sending an email.


  1. Create a file named app.yaml with the following contents:

    runtime: nodejs
    env: flex
      SPARKPOST_API_KEY: your-sparkpost-api-key

app.yaml describes how to deploy your app to Google App Engine. Read more about that here.

  1. Run the following command to deploy your app:

    gcloud app deploy
  2. Visit http://YOUR_PROJECT_ID.appspot.com to try sending email. YOUR_PROJECT_ID is the project ID you created earlier in the Google Cloud Platform Console.

See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides


Request a Tutorial

Ask for community help


GCP Tutorials

Tutorials published by GCP


Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.