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

**URL:** https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360
**Category:** Support
**Created:** [May 6, 2022, 6:16pm UTC](https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360 "2022-05-06T18:16:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![muskansingh65553](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muskansingh65553/32/259306_2.png) [@muskansingh65553](https://meta.discourse.org/u/muskansingh65553)
#### Post date: [May 6, 2022, 6:16pm UTC](https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360/1 "2022-05-06T18:16:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 6, 2022, 8:03pm UTC](https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360/2 "2022-05-06T20:03:56Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![muskansingh65553](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muskansingh65553/32/259306_2.png) [@muskansingh65553](https://meta.discourse.org/u/muskansingh65553)
#### Post date: [May 7, 2022, 8:59am UTC](https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360/3 "2022-05-07T08:59:51Z")

</div>

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 = requests.post(‘[https://domain.com/uploads.json](https://domain.com/uploads.json)’,  
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 = requests.post(f’[https://domain.com/users/{Api\_Username}/preferences/avatar/pick](https://domain.com/users/%7BApi_Username%7D/preferences/avatar/pick)’,  
headers=headers2,  
data={‘upload\_id’:3,‘type’:‘uploaded’})

# gives 404

---

<div class="post-metadata">

### Author: ![muskansingh65553](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muskansingh65553/32/259306_2.png) [@muskansingh65553](https://meta.discourse.org/u/muskansingh65553)
#### Post date: [May 7, 2022, 6:25pm UTC](https://meta.discourse.org/t/how-to-upload-change-user-profile-pic-with-api-in-python/226360/4 "2022-05-07T18:25:48Z")

</div>

@pfaffman Please check this code and guide me
