0
# App Management
1
2
Core Firebase application lifecycle management including initialization, configuration, and cleanup. The App class serves as the entry point for all Firebase services, managing credentials and providing the foundation for Firebase service access.
3
4
## Capabilities
5
6
### App Initialization
7
8
Initialize Firebase applications with credentials and configuration options. Applications can be initialized with service account credentials, application default credentials, or OAuth2 refresh tokens.
9
10
```python { .api }
11
def initialize_app(credential=None, options=None, name='[DEFAULT]'):
12
"""
13
Initializes and returns a new App instance.
14
15
Creates a new App instance using the specified options and app name.
16
If an instance already exists by the same app name, a ValueError is raised.
17
If options are not provided, an attempt is made to load options from the
18
FIREBASE_CONFIG environment variable.
19
20
Args:
21
credential: A credential object used to initialize the SDK (optional).
22
If none is provided, Google Application Default Credentials are used.
23
options: A dictionary of configuration options (optional). Supported options
24
include 'databaseURL', 'storageBucket', 'projectId',
25
'databaseAuthVariableOverride', 'serviceAccountId' and 'httpTimeout'.
26
name: Name of the app (optional). Defaults to '[DEFAULT]'.
27
28
Returns:
29
App: A newly initialized instance of App.
30
31
Raises:
32
ValueError: If the app name is already in use, or any of the provided
33
arguments are invalid.
34
"""
35
```
36
37
### App Retrieval
38
39
Retrieve existing Firebase application instances by name for use across different parts of your application.
40
41
```python { .api }
42
def get_app(name='[DEFAULT]'):
43
"""
44
Retrieves an App instance by name.
45
46
Args:
47
name: Name of the App instance to retrieve (optional).
48
Defaults to '[DEFAULT]'.
49
50
Returns:
51
App: An App instance with the given name.
52
53
Raises:
54
ValueError: If the specified name is not a string, or if the specified
55
app does not exist.
56
"""
57
```
58
59
### App Cleanup
60
61
Gracefully delete Firebase applications and clean up associated resources including active service connections.
62
63
```python { .api }
64
def delete_app(app):
65
"""
66
Gracefully deletes an App instance.
67
68
This will clean up any services associated with the App and prevent
69
further use of the App instance.
70
71
Args:
72
app: The app instance to be deleted.
73
74
Raises:
75
ValueError: If the app is not initialized or not of type App.
76
"""
77
```
78
79
## Credential Management
80
81
Firebase Admin SDK supports multiple credential types for different authentication scenarios.
82
83
### Service Account Credentials
84
85
```python { .api }
86
class Certificate:
87
"""Service account certificate credential."""
88
89
def __init__(self, cert):
90
"""
91
Initialize with service account certificate.
92
93
Args:
94
cert: Path to service account JSON file or certificate dict
95
"""
96
97
@property
98
def project_id(self):
99
"""The project ID from the service account."""
100
101
@property
102
def service_account_email(self):
103
"""The service account email address."""
104
```
105
106
### Application Default Credentials
107
108
```python { .api }
109
class ApplicationDefault:
110
"""Google Application Default Credentials."""
111
112
def __init__(self):
113
"""Initialize with application default credentials."""
114
115
@property
116
def project_id(self):
117
"""The project ID from the credential."""
118
```
119
120
### OAuth2 Refresh Token
121
122
```python { .api }
123
class RefreshToken:
124
"""OAuth2 refresh token credential."""
125
126
def __init__(self, refresh_token):
127
"""
128
Initialize with refresh token.
129
130
Args:
131
refresh_token: Path to refresh token JSON file or token dict
132
"""
133
134
@property
135
def client_id(self):
136
"""The OAuth2 client ID."""
137
138
@property
139
def client_secret(self):
140
"""The OAuth2 client secret."""
141
142
@property
143
def refresh_token(self):
144
"""The refresh token string."""
145
```
146
147
## App Configuration
148
149
### Configuration Options
150
151
```python { .api }
152
# Valid configuration keys
153
CONFIG_KEYS = [
154
'databaseAuthVariableOverride', # Custom auth variable override
155
'databaseURL', # Realtime Database URL
156
'httpTimeout', # HTTP request timeout in seconds
157
'projectId', # Google Cloud project ID
158
'storageBucket' # Cloud Storage bucket name
159
]
160
```
161
162
### Environment Configuration
163
164
The SDK can automatically load configuration from the `FIREBASE_CONFIG` environment variable:
165
166
```python
167
# JSON string format
168
export FIREBASE_CONFIG='{"projectId":"my-project","databaseURL":"https://my-project-default-rtdb.firebaseio.com"}'
169
170
# File path format
171
export FIREBASE_CONFIG='/path/to/firebase-config.json'
172
```
173
174
## Usage Examples
175
176
### Basic Initialization
177
178
```python
179
import firebase_admin
180
from firebase_admin import credentials
181
182
# Initialize with service account
183
cred = credentials.Certificate("serviceAccountKey.json")
184
app = firebase_admin.initialize_app(cred)
185
```
186
187
### Multiple Apps
188
189
```python
190
# Initialize multiple apps
191
default_app = firebase_admin.initialize_app(cred)
192
secondary_app = firebase_admin.initialize_app(cred, name='secondary')
193
194
# Retrieve apps
195
app1 = firebase_admin.get_app() # Default app
196
app2 = firebase_admin.get_app('secondary')
197
```
198
199
### Configuration Options
200
201
```python
202
app = firebase_admin.initialize_app(cred, {
203
'databaseURL': 'https://my-project-default-rtdb.firebaseio.com/',
204
'storageBucket': 'my-project.appspot.com',
205
'httpTimeout': 60
206
})
207
```
208
209
## Types
210
211
```python { .api }
212
class App:
213
"""Firebase application instance."""
214
215
@property
216
def name(self):
217
"""The name of this App instance."""
218
219
@property
220
def credential(self):
221
"""The credential used to initialize this App."""
222
223
@property
224
def options(self):
225
"""The configuration options for this App."""
226
227
@property
228
def project_id(self):
229
"""The Google Cloud project ID associated with this App."""
230
231
class AccessTokenInfo:
232
"""OAuth2 access token information."""
233
234
def __init__(self, access_token, expiry):
235
"""
236
Initialize access token info.
237
238
Args:
239
access_token: The access token string
240
expiry: Token expiration datetime
241
"""
242
```