Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    Build simple backends with Gatsby Serverless Functions

    Until recently adding a little backend to your Gatsby site meant 2 options:

    1. Climbing the AWS learning curve
    2. Rewrite with NextJS

    With version 3.7 Gatsby gained the /api directory. Pop a JavaScript or TypeScript file in src/api, export a handler, and you've got a serverless function.

    // src/api/hello.js
    module.exports = function handler(req, res) {
      return res.status(200).json({ hello: req.query.name })
    }
    

    You can call that as an API request from your site. Great for 3rd party integrations with secret tokens, user reactions, authentication, and anything else you can't do in the browser.

    I joined Queen Raae on a bonus episode of her Gatsby Summer Functions webinar series to explore how all this works.

    Architecting your Gatsby Serverless Functions
    Architecting your Gatsby Serverless Functions

    We started with my crappy implementation of a function that takes a form submission and:

    • sends an email
    • sends a few texts
    • saves your message to a firestore

    We refactored this serverless function so it's faster and more reliable. While refactoring we talked about:

    • how to architect your functions
    • when to put code on the frontend or the backend
    • why distributed transactions are hard
    • when you should worry about absolute correctness
    • when's it make sense to leverage AWS
    • how using small functions gives you greater flexibility

    And I drew a sketch, live 🤘

    Enjoy the webinar, hit reply if you have questions.

    And if you'd like a deeper introduction to serverless, the Serverless Handbook is full of sketches like that.

    Cheers,
    ~Swizec

    PS: yes, there's options between AWS and NextJS like Firebase or Netlify cloud functions. And even tougher climbs like hosting your own servers. Adding those made the intro too long ✌️

    Published on August 24th, 2021 in Serverless, Podcasts, Gatsby

    Did you enjoy this article?

    Continue reading about Build simple backends with Gatsby Serverless Functions

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️