Skip to content

Deployment Guide

This guide provides the steps to deploy the API server for production use. The recommended method is to build a Docker container and host it on a cloud service that supports containers.

  1. Configure Production Environment Variables

    Before building your application for production, you must configure your environment variables. Unlike the .env file used for local development, production environments should use the hosting provider’s system for managing secrets (e.g., GitHub Secrets for build pipelines, or environment variable settings in your hosting dashboard).

    The following variables are required for production:

    • DATABASE_URL: The connection string for your production MongoDB database.
    • JWT_SECRET_KEY: A long, complex, and secret string for signing authentication tokens.
    • SENDGRID_API_KEY: Your API key for the SendGrid service.
    • DEFAULT_SENDER_EMAIL: The email address used to send transactional emails (like OTP codes).
    • OTP_TEMPLATE_ID: The ID of your dynamic email template in SendGrid.

    You should also set JWT_ISSUER to the public URL of your API.

  2. Build the Docker Container

    The API server repository includes a Dockerfile that simplifies the process of creating a production-ready container. This file handles setting up the Dart environment, installing dependencies, and creating an optimized production build of the server.

    To build the container locally, run the following command from the root of the flutter-news-app-api-server-full-source-code directory:

    Terminal window
    docker build -t your-api-server-image .

    Replace your-api-server-image with a name for your Docker image.

  3. Deploy to a Hosting Provider

    Once you have built the Docker image, you can deploy it to any hosting provider that supports Docker containers. You will typically push your image to a container registry (like Docker Hub, Google Container Registry, or Amazon ECR) and then deploy it from there.

    Popular choices for hosting Docker containers include:

    Follow your chosen provider’s documentation for deploying a container from an image or a Dockerfile. During the setup process on the hosting platform, you will need to configure the environment variables mentioned in Step 1.