Realm Administration API - DELETE user

Hi there,

I’m using Realm and I would like to delete an App User programmatically.
I’m trying to do so by using Realm Administration API, especially these methods:

When I use the GET method everything is fine, I’m able to retrieve my user information, however the DELETE method isn’t working.

Here is what I do to call the GET method:

curl --request GET --header ‘Authorization: Bearer <access_token>’ https://realm.mongodb.com/api/admin/v3.0/groups/<group_id>/apps/<app_id>/users/<user_id>

And I do exactly the same to delete but just replacing “GET” by “DELETE”.

Is there something I missed?

Thanks for your help!

1 Like

Have you tried this endpoint yet? It uses projectId as opposed to groupdId

(working in node.js)

https://realm.mongodb.com/api/admin/v3.0/groups/${projectId}/apps/${appId}/users/${userId}

I’m actually using my projectId as goupId because in this link it’s said that it’s the same.

Maybe you can get more specific about how the DELETE request is not working?

Well unfortunately there is not much to say. I’m running the curl command in order to delete one of the following Realm App User, using the id column for <user_id>:

As the DELETE request returns nothing I don’t have any clues about what is going wrong.

Tell me if you want some specific information.

Hey @Julien_Chouvet - a few things to note:
a) the DELETE method isn’t going to return anything - I just tried the following which successfully deleted my user:

curl --request DELETE \
  --header 'Authorization: Bearer <accesstoken>' \
https://realm.mongodb.com/api/admin/v3.0/groups/<projectID>/apps/<appID>/users/<userId>

Project ID and Group ID are indeed the same thing and used interchangeably. If you’re using the project ID where your app is located, it shouldn’t cause any issues.

If you’re looking for much easier user management, we just introduced it in our new CLI - you can download it via:
npm i -g mongodb-realm-cli@beta

after logging in via the CLI, you can run

realm-cli users delete which will provide a list of users that you can select to delete:

Let me know if you have any other questions

  • Sumedha
1 Like

Hey @Sumedha_Mehta1,

Thanks for your help but unfortunately it’s still not working :confused:
I tried again with your curl command but it did not work (but still working with GET). Is there somewhere some logs produced by the DELETE request that I can use to find what is going wrong?

Actually I want to use this REST method on my iOS app to allow a user to remove its account. I first tried directly to do the DELETE from the app but it didn’t work that’s why I’m trying with curl.

So, is there another way to delete a user that I can use from my app? Is it possible to do it from a Realm function or with a Trigger?

Thanks for your help!

1 Like

Hey Julien - you should be able to do this in a system function or trigger as well. We don’t typically recommend that you do admin API calls from the client, but you could do something like this by calling a system function that executes this API call.

Pavel goes through an example here of how to call the Admin API in functions - Custom Function Authentication Problems and Solutions - #2 by Pavel_Duchovny

Can I ask what provider this user has been registered on ream with? (email/pass, anon, etc…)

1 Like

Ok thank! I’ll try this way.

I tried with both email/password & custom function

Hey @Sumedha_Mehta1,

I tried to create a webhook to call the Admin API to delete a user but it still doesn’t work.
I follow the example you gave me:

The GET works perfectly but the DELETE still does nothing.
Here is my code:

exports = function(payload, response) {

  const AtlasPrivateKey = <my_private_key>
  const AtlasPublicKey = <my_public_key>
  const AtlasGroupId = <my_group_id>
  const appId = <my_app_id>
  
  // Authenticate to Realm API
  return context.http.post({
    url : "https://realm.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login",
    headers : { "Content-Type" : ["application/json"],
                 "Accept" : ["application/json"]},
    body : {"username": AtlasPublicKey, "apiKey": AtlasPrivateKey},
    encodeBodyAsJSON: true
    }).then (respone_cloud_auth => {
    const cloud_auth_body = JSON.parse(respone_cloud_auth.body.text());
    
        // Get the internal appId
       return context.http.get({
       url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps`,
       headers : { "Content-Type" : ["application/json"],
                    "Accept" : ["application/json"],
                    "Authorization" : [`Bearer ${cloud_auth_body.access_token}`]
       }
       }).then(respone_realm_apps => {
          const realm_apps = JSON.parse(respone_realm_apps.body.text());
   
          var internalAppId = "";
   
         realm_apps.map(function(app){ 
         if (app.client_app_id == appId){
             internalAppId = app._id;
         }
       });
    
        return context.http.delete({
        url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps/${internalAppId}/users/5ee8bc87c6be46a85871365a`,
        headers : { "Content-Type" : ["application/json"],
                     "Accept" : ["application/json"],
                     "Authorization" : [`Bearer ${cloud_auth_body.access_token}`]}
        }).then (respone_realm_users => {
            return respone_realm_users.body.text();
        });
  });
  });
}  

For <my_group_id> I use the id provided here:

And for <my_app_id> I use the id provided here:

Thanks!

Hey Julien - interesting that the GET works perfectly. The app id you’re using should be the second string in your URL when you’re in Realm, not the app id used for connecting via the SDKs. Does that work in your case?

or you can do a request to get it like described here
https://docs.mongodb.com/realm/admin/api/v3/#application-id

Yes this is the one i’m actually using in my request. In the code below I used the app id <my_app_id> in order to retrieve the ‘internal’ app id thanks to this part of the code:

 // Get the internal appId
       return context.http.get({
       url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps`,
       headers : { "Content-Type" : ["application/json"],
                    "Accept" : ["application/json"],
                    "Authorization" : [`Bearer ${cloud_auth_body.access_token}`]
       }
       }).then(respone_realm_apps => {
          const realm_apps = JSON.parse(respone_realm_apps.body.text());
   
          var internalAppId = "";
   
         realm_apps.map(function(app){ 
         if (app.client_app_id == appId){
             internalAppId = app._id;
         }
       });

I checked the string in the var internalAppId and it’s the same as the one in the URL when I’m in my Realm.

The solution to this issue was that the API Key did not have ‘Project Owner’ permissions and mutations to the app were not permitted.

3 Likes

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