Uploading files to Firebase Storage using REST API

Firebase Storage uses Google Cloud Storage under the hood, so you can use the GCS REST API to get 90% of the way there. (Docs here.)

There's a couple differences.

  1. Download URLs won't be automatically generated until you access it through the Firebase Storage SDK.
  2. You won't be able to use Firebase Authentication to upload through the GCS REST endpoint. You'll either need to set up OAuth (which, you might be able to get through Google Sign in with Authentication) or a service account. (Here's an example of how you can authenticate from Node.js to GCS.)

It is possible to upload a document to Firebase storage using the REST API. I have implemented in Python with the requests library to set HTTP headers and make a POST request. The process should be similar in other languages or with cURL.

To upload without auth:

def firebase_upload():
"""
Executes a POST request to upload an image to Firebase Storage.
DEMONSTRATION ONLY, USE NO FURTHER!
args: None
returns: response (json) with the response to the request
usage: res = firebase_upload()
"""
    response = None

    file2upload = "/Your/Path/To/your_pic.png"
    file_binary = open(file2upload, "rb").read()

    # HTTP
    url2file = 'https://firebasestorage.googleapis.com/v0/b/<your-project-ID.appspot.com>/o/stash%2Fyour_pic.png'
    headers = {"Content-Type": "image/png"}

    r = requests.post(url2file, data=file_binary, headers=headers)
    response = r.json()

    return response

This uploads the image to a folder named 'stash'. Be sure to use %2F instead of forward-slash in the URL. Also, you will need to make your storage bucket public using Firebase Rules. A successful response will return JSON like the following:


    {'name': 'stash/your_pic.png',
    'bucket': '<your-project-ID>.appspot.com',
    'generation': '1608437408388390',
    'metageneration': '1',
    'contentType': 'image/png',
    'timeCreated': '2020-12-20T04:10:08.388Z',
    'updated': '2020-12-20T04:10:08.388Z',
    'storageClass': 'STANDARD',
    'size': '52628',
    'md5Hash': 'mmkqwEek6tMAZmvooQ9X7g==',
    'contentEncoding': 'identity',
    'contentDisposition': "inline; filename*=utf-8''your_pic.png",
    'crc32c': 'fhlSmw==',
    'etag': 'CKaq+6LY2+0CEAE=',
    'downloadTokens': '<secure_token>'}

To upload with auth, the procedure is the same except you pass an auth token (obtained using the REST end-point for auth) with the HTTP header. Modify one line of code in the function like so.

    headers = {"Content-Type": "image/png", "Authorization": "Bearer "+auth_token}

The download URL is the url2file with the addition of 'downloadTokens' from the API response. Add: '?alt=media&token=' followed by the token string.

The example shows how to add an image to Firebase storage and all CRUD operations can be performed with the REST API by tweaking this pattern.