How to setup your own public API using a free cloud service
As a mobile developer (like me), if you have ever started a personal project to study something or build a professional showcase, it’s common to look for a public API. This simple and tedious task of googling “public API” makes us spend a lot of time choosing a good enough one. At the same time, you may have the desire to own the API, to release your creativity and produce a personalized response.
I’m here to help you with a free solution. No more public movies API! 😂
TL;DR
This article will cover how to setup a static resource provider service using the free tier of Firebase Storage.
Requisites
- Provide static content
- Public API
Static content is any file that is stored in a server and is the same every time it is delivered to users. Text files and images are examples of this kind of content.
I’ll use a JSON text file and some images as an example for this guide.
Google Firebase
My choice is the Cloud Storage service of Firebase because of a few and relevant reasons:
- Backup by one of the biggest cloud players: Google!
- Has a free tier "Spark" with generous limits
- The possibility to upgrade to a paid plan if necessary
- Hard limit for billing. You will not be charged by exceeding the free plan limits
Setup Project
- Go to the Firebase site and click "Get Started"
- Create a new Project. Click the super large "+ add Project" button
- Choose a meaningful name that relates to your project
- The next screen has an option for analytics. I set to disabled because it won't be necessary
- Then the project will be created. It may take a while to complete
Setup bucket
After the project creation, it redirects you to the main Firebase Console page.
- Click "Storage" on the left menu panel
- Then, hit "Get started"
3. The first screen, give you awareness of the read/write permission rule. The default rule requires an authentication token.
4. Set Cloud Storage Location. I don't think it will be necessary to change but you may read the "Learn More"
Upload files
Now upload a few files for testing. Clicking on an item, a side panel will open with the details. The name link will open the item in a new tab.
You'll notice the URL pattern. This is the carrot_cake.jpg URL:
https://firebasestorage.googleapis.com/v0/b/mediumarticle-f6290.appspot.com/o/carrot_cake.jpg?alt=media&token=775b1585-ae47-447e-a3c7-c3cbdc31ce74
Note that we have two undesirable query parameters. We could remove token
by making the storage public but alt=media
would be still required.
Accessing it using Google Cloud Platform
There isn't a Firebase setting to change the requirement of alt=media
but we may use Google Cloud Platform interface to access our firebase storage.
- Open your GCP project Console
- Go to "Cloud Storage" on the left menu
- Click on the bucket (same name from Firebase)
- Go to "Permissions" tab and then "ADD" button. The "Add Members" dialog box will appear.
5. In the "New members" field, enter allUsers
.
6. In the "Select a role" drop-down, select the "Cloud Storage" sub-menu and click the "Storage Object Viewer" option.
7. Click "Save".
We're done! Now all objects are Public to internet and we can use the GCP domain. Check the URL:
https://storage.googleapis.com/mediumarticle-f6290.appspot.com/carrot_cake.jpg
Notes
- If you want to publish an app, I recommend buying a domain and consider using a CDN for static resources
- I have also tried Amazon S3. It's interesting but it is only free for 1 year and we can't set billing limits.
That’s it! Ready to use!