Edit on GitHub
Report issue
Page history

Send email with SparkPost and Node.js on App Engine flexible environment

Author(s): @ewandennis ,   Published: 2017-03-22

Contributed by the Google Cloud community. Not official Google documentation.

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


  1. Create a project in the Cloud 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 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 App Engine. Read more about that here.

  2. Run the following command to deploy your app:

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

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

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.