Debian Codegraph @

v5_MIGRATION_GUIDE.md

raw symbols SCIP
1 # v5 Migration Guide
2
3 A guide to migrating the Auth0 Python SDK from v4 to v5.
4
5 - [Overall changes](#overall-changes)
6 - [Python versions](#python-versions)
7 - [Authentication API](#authentication-api)
8 - [Management API](#management-api)
9 - [Specific changes to the Management API](#specific-changes-to-the-management-api)
10 - [Client initialization](#client-initialization)
11 - [Return types: Pydantic models](#return-types-pydantic-models)
12 - [Pagination](#pagination)
13
14 ## Overall changes
15
16 ### Python versions
17
18 v5 supports Python 3.9 and above.
19
20 ### Authentication API
21
22 This major version change does not affect the Authentication API. Any code written for the Authentication API in v4 should work in v5.
23
24 ### Management API
25
26 v5 introduces significant improvements to the Management API by migrating to [Fern](https://github.com/fern-api/fern) as our code generation tool. This brings several benefits:
27
28 - **Type safety**: All responses are now Pydantic models with full type hints.
29 - **IDE support**: Autocomplete and inline documentation for all API methods and response fields.
30 - **Automatic token management**: Built-in support for client credentials with automatic token caching and refresh.
31 - **Improved pagination**: New `SyncPager` and `AsyncPager` classes for easy iteration across paginated results.
32 - **Better resource organization**: Sub-resources are organized into sub-clients for a more intuitive API structure.
33
34 ## Specific changes to the Management API
35
36 ### Client initialization
37
38 v5 introduces a `ManagementClient` class with support for both token-based and client credentials authentication.
39
40 #### Token-based authentication
41
42 ```python
43 # v4
44 from auth0.management import Auth0
45
46 domain = 'your-tenant.auth0.com'
47 token = 'MGMT_API_TOKEN'
48 auth0 = Auth0(domain, token)
49
50 # Access users
51 user = auth0.users.get('user_id')
52 ```
53
54 ```python
55 # v5
56 from auth0.management import ManagementClient
57
58 client = ManagementClient(
59 domain="your-tenant.auth0.com",
60 token="MGMT_API_TOKEN",
61 )
62
63 # Access users
64 user = client.users.get("user_id")
65 ```
66
67 #### Client credentials authentication (new in v5)
68
69 v5 supports automatic token management when using client credentials. The token is automatically fetched, cached, and refreshed before expiration:
70
71 ```python
72 # v5 with client credentials
73 from auth0.management import ManagementClient
74
75 client = ManagementClient(
76 domain="your-tenant.auth0.com",
77 client_id="YOUR_CLIENT_ID",
78 client_secret="YOUR_CLIENT_SECRET",
79 )
80
81 # Token is automatically fetched and cached
82 # No need to manually obtain or refresh tokens
83 user = client.users.get("user_id")
84 ```
85
86 #### Configuration options
87
88 ```python
89 # v4
90 from auth0.management import Auth0
91 from auth0.rest import RestClientOptions
92
93 options = RestClientOptions(
94 telemetry=True,
95 timeout=5.0,
96 retries=3
97 )
98 auth0 = Auth0(domain, token, rest_options=options)
99 ```
100
101 ```python
102 # v5
103 from auth0.management import ManagementClient
104
105 client = ManagementClient(
106 domain="your-tenant.auth0.com",
107 token="MGMT_API_TOKEN",
108 timeout=60.0, # Timeout in seconds
109 headers={"X-Custom-Header": "value"}, # Custom headers
110 )
111 ```
112
113 ### Return types: Pydantic models
114
115 The most significant change is that all API responses are now **Pydantic models** instead of raw dictionaries. This provides type safety, IDE autocomplete, and validation.
116
117 #### Accessing response data
118
119 ```python
120 # v4: Dictionary access
121 user = auth0.users.get('user_id')
122 print(user['email']) # Dict key access
123 print(user.get('nickname')) # Safe access with .get()
124
125 # Check if key exists
126 if 'app_metadata' in user:
127 print(user['app_metadata'])
128 ```
129
130 ```python
131 # v5: Attribute access
132 user = client.users.get("user_id")
133 print(user.email) # Attribute access
134 print(user.nickname) # Direct attribute access (None if not present)
135
136 # Access nested data
137 if user.app_metadata:
138 print(user.app_metadata)
139 ```
140
141 #### Converting to dictionaries
142
143 If you need dictionary access for compatibility with existing code, use `model_dump()`:
144
145 ```python
146 # v5: Convert to dict
147 user = client.users.get("user_id")
148 user_dict = user.model_dump() # Returns a dictionary
149
150 # Now you can use dict-style access
151 print(user_dict['email'])
152 ```
153
154 #### Benefits of Pydantic models
155
156 1. **IDE autocomplete**: Type `user.` and see all available fields
157 2. **Type checking**: Catch errors at development time with mypy or pyright
158 3. **Validation**: Data is validated against the schema
159 4. **Immutability**: Response objects are frozen by default
160
161 ### Pagination
162
163 v5 introduces `SyncPager` (and `AsyncPager` for async) to simplify working with paginated endpoints.
164
165 #### v4 pagination pattern
166
167 ```python
168 # v4: Manual pagination
169 all_users = []
170 page = 0
171
172 while True:
173 result = auth0.users.list(page=page, per_page=50, include_totals=True)
174 all_users.extend(result['users'])
175
176 if len(all_users) >= result['total']:
177 break
178 page += 1
179
180 # Process all users
181 for user in all_users:
182 print(user['email'])
183 ```
184
185 #### v5 pagination patterns
186
187 **Automatic iteration** - The simplest approach, iterates across all pages automatically:
188
189 ```python
190 # v5: Automatic iteration across all pages
191 for user in client.users.list():
192 print(user.email)
193 ```
194
195 **Page-by-page iteration** - When you need to process pages individually:
196
197 ```python
198 # v5: Page-by-page iteration
199 for page in client.users.list().iter_pages():
200 print(f"Processing {len(page.items)} users")
201 for user in page.items:
202 print(user.email)
203 ```
204
205 **Manual pagination** - For explicit control over pagination:
206
207 ```python
208 # v5: Manual pagination with explicit parameters
209 pager = client.users.list(page=0, per_page=50)
210
211 # Access current page items
212 for user in pager.items:
213 print(user.email)
214
215 # Check if there are more pages
216 if pager.has_next:
217 next_pager = pager.next_page()
218 for user in next_pager.items:
219 print(user.email)
220 ```
221
222 #### Async pagination
223
224 For async code, use `AsyncManagementClient` with async iteration:
225
226 ```python
227 # v5: Async pagination
228 from auth0.management import AsyncManagementClient
229
230 client = AsyncManagementClient(
231 domain="your-tenant.auth0.com",
232 token="MGMT_API_TOKEN",
233 )
234
235 # Async iteration
236 async for user in client.users.list():
237 print(user.email)
238
239 # Async page-by-page
240 async for page in client.users.list().iter_pages():
241 for user in page.items:
242 print(user.email)
243 ```