Deploy to your site using the Hosting REST API

The Firebase Hosting REST API enables
programmatic and customizable deployments to your Firebase-hosted sites.
Use this REST API to deploy new or updated hosting configurations and content
files.

As an alternative to using the
Firebase command line interface (CLI) for
deployments, you can use the Firebase Hosting REST API to programmatically
create a new version of
assets for your site, upload files to the version, then deploy the version to
your site.

For example, with the Firebase Hosting REST API, you can:

Schedule deploys. By using the REST API in conjunction with a cron job,
you can change Firebase-hosted content on a regular schedule (for example, to
deploy a special holiday or event-related version of your content).

Integrate with developer tools. You can create an option in your tool to
deploy your web app projects to Firebase Hosting using just one click (for
example, clicking a deploy button within an IDE).

Automate deploys when static content is generated. When a process
generates static content programmatically (for example, user-generated content
such as a wiki or a news article), you can deploy the generated content as
static files rather than serving them dynamically. This saves you expensive
compute power and serves your files in a more scalable way.

This guide first describes how to enable, authenticate, and authorize the API.
Then this guide walks through an example to create a Firebase Hosting
version, to upload required files to the version, then finally to deploy the
version.

Before you begin: Enable the REST API

Note: Every Firebase project has a corresponding project in the Google APIs
console.

Click Enable on the Firebase Hosting API page.

Step 1: Get an access token to authenticate and authorize API requests

Firebase projects support Google
service accounts,
which you can use to call Firebase
server APIs from your app server or trusted environment. If you're developing
code locally or deploying your application on-premises,
you can use credentials obtained
via this service account to authorize server requests.

To authenticate a service account and authorize it
to access Firebase services, you must generate a private key file in JSON
format.

After your access token expires, the token refresh method is called
automatically to retrieve an updated access token.

Note: While using a service account is appropriate for automated tasks in
server environments, there are other ways to obtain authorization to use the
Firebase Hosting REST API. For example, project members can use the API if
they (1) are assigned the Admin or Editor role for a project and (2) provide
access tokens with the appropriate scope. Review the
other scenarios
for different OAuth flows that will allow you to obtain authorization to use the
Firebase Hosting REST API.

Step 2: Create a new version for your site

Your first API call is to create a new
Version for your site.
Later in this guide, you’ll upload files to this version, then deploy it to your
site.

Determine the site-name of the site to which you want to deploy.

Note: For your default Hosting site, the
site-name is your Firebase project ID (which is used to create
your Firebase subdomains site-name.web.app and
site-name.firebaseapp.com).
If you’ve created multiple sites in
your Firebase project, make sure that you’re using the site-name
of the site to which you’d like to deploy.

This response contains a unique identifier for the new version, in the format:
sites/site-name/versions/version-id. You’ll
need this unique identifier throughout this guide to reference this specific
version.

Step 3: Specify the list of files you want to deploy

Now that you have your new version identifier, you need to tell
Firebase Hosting which files you want to eventually deploy in this new
version.

Important: In this call, you’ll need to list all files for the version,
including new files and existing files from previous versions (both modified
and unmodified).

This API requires that you identify files by a SHA256 hash. So, before you can
make the API call, you’ll first need to calculate a hash for each static file by
Gzipping the files then taking the SHA256 hash of each newly compressed file.

For this example, let's say that you want to deploy three files in the new
version: file1, file2, and file3.

The hash of each file that needs to be uploaded. For instance, in this
example file1 had already been uploaded in a previous version, so its hash
is not included in the uploadRequiredHashes list.

Note: Some files in your new version don’t need to be uploaded, for example,
if the file was already in a previous version and is unmodified for the new
version.

The uploadUrl which is specific to the new version.

In the next step to upload the two new files, you’ll need the hashes and the
uploadURL from the versions.populateFiles response.

Step 4: Upload required files

You need to individually upload each required file (those files which are listed
in uploadRequiredHashes from the versions.populateFiles response in the
previous step). For these file uploads, you’ll need the file hashes and the
uploadUrl from the previous step.

Append a forward slash and the hash of the file to the uploadUrl to
create a file-specific URL in the format:
https://upload-firebasehosting.googleapis.com/upload/sites/site-name/versions/version-id/files/file-hash.

Upload all the required files one-by-one (in this example, only file2.gz
and file3.gz) to the file-specific URL using a series of requests.

Step 6: Release the version for deployment

Now that you have a finalized version, release it for deployment. For this step,
you need to create a
Release of your version
that contains the hosting configuration and all the content files for your new
version.