How to Upload / Change user profile pic with API in Python

After checking all posts, I tried all solutions, but none of them worked.
Can anyone help me?

1 Like

Welcome, Muskan!

Are you trying to use the Discourse API to change a user’s profile pic?

You’ll need to say more about what you tried to do, including the code that you expected to work and what happened when you ran it.

Uploading an image via the API is a bit tricky, so you’ll want to make sure that you know how to do something easier first (like change the user’s name or location).


Thank You for the reply!

I already created 500+ activated users with API

here is the code I am using for image upload

headers1 = {
‘Api-Key’: key,
‘Api-Username’: username,
headers2 = {
‘Api-Key’: key,
‘Api-Username’: username,
‘Content-Type’: ‘application/json;’,
response1 =‘’,
files = {‘files’: (‘image.png’, open(f’{file_path}’,‘rb’), ‘image/png’)},
data={‘type’:‘avatar’,‘user_id’:9 ,‘synchronous’: ‘true’},
headers=headers1 )

getting upload_id from response

response2 =’{Api_Username}/preferences/avatar/pick’,

gives 404

@pfaffman Please check this code and guide me