Connecting to Firebase
Set up Firebase in your Angular app — create a Firebase project, install AngularFire, and configure the connection.
Connecting to Firebase
Firebase is a backend-as-a-service by Google. It provides a real-time database, authentication, hosting, and more — all without needing to build your own server. Let's connect our Angular app to Firebase.
Step 1: Create a Firebase Project
- Go to console.firebase.google.com
- Click "Add Project"
- Give your project a name (e.g., "my-angular-app")
- Click "Create Project"
Step 2: Get Your Firebase Config
- In the Firebase Console, click the gear icon → Project Settings
- Scroll to "Your apps" → Click the web icon (
</>) - Register an app and copy the config object
// Your Firebase config will look like this:
const firebaseConfig = {
apiKey: "AIzaSy...",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
projectId: "my-app",
storageBucket: "my-app.appspot.com",
messagingSenderId: "123456789",
appId: "1:123456789:web:abc123"
};
Step 3: Set Up Firebase Realtime Database
- In the Firebase Console, go to Realtime Database
- Click "Create Database"
- Choose "Start in test mode" (for development)
Test mode rules (allows read/write for 30 days — for development only):
{
"rules": {
".read": true,
".write": true
}
}
Step 4: Store Config in Environment
// src/environments/environment.ts
export const environment = {
production: false,
firebase: {
apiKey: "AIzaSy...",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app-default-rtdb.firebaseio.com",
projectId: "my-app",
storageBucket: "my-app.appspot.com",
messagingSenderId: "123456789",
appId: "1:123456789:web:abc123"
}
};
Step 5: Configure HttpClient for Firebase
For simple REST access to Firebase's Realtime Database, you can use Angular's HttpClient directly — no extra libraries needed:
// app.module.ts
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
AppRoutingModule,
],
// ...
})
Firebase REST API Endpoints
Firebase's Realtime Database exposes a REST API at your databaseURL:
// Base URL format:
https://your-project.firebaseio.com/
// Access a collection:
https://your-project.firebaseio.com/posts.json
// Access a specific item:
https://your-project.firebaseio.com/posts/-NxAbc123.json
The .json extension is required — it tells Firebase to return JSON data.
Testing the Connection
// test in a component
import { HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment';
export class TestComponent implements OnInit {
dbUrl = environment.firebase.databaseURL;
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Write test data
this.http.put(`${this.dbUrl}/test.json`, { message: 'Hello Firebase!' })
.subscribe(res => console.log('Written:', res));
// Read it back
this.http.get(`${this.dbUrl}/test.json`)
.subscribe(data => console.log('Read:', data));
}
}
Key Takeaways
- Firebase provides a backend without building a server
- Create a project at console.firebase.google.com
- Store the Firebase config in your environment file
- Use
HttpClientto talk to Firebase's REST API - Add
.jsonto all Firebase REST URLs - Use test-mode rules during development (switch to secure rules for production)