# Can we update email of user using api

**URL:** <https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [January 23, 2024, 11:49am UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450 "2024-01-23T11:49:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![prems](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@prems](https://community.auth0.com/u/prems)\
**Post date:** [January 23, 2024, 11:49am UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450/1 "2024-01-23T11:49:22Z")

</div>

```
    private async Task UpdateAuthUserEmail(string Auth0Id,string _newEmail)
    {
        // Replace these values with your Auth0 information
        string domain = _auth0Config.Domain;
        string clientId = _auth0Config.ClientId;
        string clientSecret = _auth0Config.ClientSecret;
        string userId = Auth0Id; // replace with the actual user ID
        string newEmail = _newEmail; // replace with the new email

        // Build the Auth0 Management API endpoint URL
        string apiUrl = $"https://{domain}/api/v2/users/{userId}";

        // Call the function to update the user's email
        await UpdateUserEmail(apiUrl, clientId, clientSecret, newEmail);

        bool Vart = true;
        // You might want to return a more meaningful result based on the success or failure of the update
    }

    private static async Task UpdateUserEmail(string apiUrl, string clientId, string clientSecret, string newEmail)
    {
        using (HttpClient client = new HttpClient())
        {
            // Set the Authorization header with the client credentials
            string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
            client.DefaultRequestHeaders.Add("Authorization", $"Basic {credentials}");

            try
            {
                // Build the JSON payload with the new email address
                string jsonPayload = $"{{\"email\": \"{newEmail}\"}}";
                HttpContent content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

                // Make a PATCH request to update the user's email
                HttpResponseMessage response = await client.PatchAsync(apiUrl, content);

                // Check if the request was successful (status code 200)
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine($"User's email updated successfully: {newEmail}");
                }
                else
                {
                    Console.WriteLine($"API Request failed with status code: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }

```

this code is correct

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [January 23, 2024, 4:06pm UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450/3 "2024-01-23T16:06:45Z")

</div>

Hi @prems

Welcome to the Auth0 Community!

Thank you for posting your question. I’ve checked your code, and there are a few things that you can consider adjusting.

1. Authentication Token  
Instead of using Basic Auth with the clientID and secret, obtain the Managment API token with proper scope.
2. Error Handling  
You can consider including error-catching and handling to log any potential issues.

[https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production)

> **[Tokens](https://auth0.com/docs/secure/tokens)**
>
> Describes the types of tokens related to identity and authentication and how they are used by Auth0.

Let me know if that’s answer your question  
Thanks

Dawid

---

<div class="post-metadata">

**Author:** ![prems](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@prems](https://community.auth0.com/u/prems)\
**Post date:** [January 24, 2024, 5:16am UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450/4 "2024-01-24T05:16:32Z")

</div>

NO this is not answer of my question.  
basically i intigrated auth0 for signup and login but. my users want to change thier email address then i want to make a functionality for edit by API

what api i will use

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [January 24, 2024, 12:18pm UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450/5 "2024-01-24T12:18:28Z")

</div>

To update a user email, you must make a PATCH call on the /users/:id endpoint with the correct body, that will contain a new email → [Auth0 Management API v2](https://auth0.com/docs/api/management/v2/users/patch-users-by-id)

Does this answer your question?

Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 7, 2024, 12:19pm UTC](https://community.auth0.com/t/can-we-update-email-of-user-using-api/125450/6 "2024-02-07T12:19:23Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
