K
Kinde14mo ago
_.Mass._

Adding an account to an organization via api returns 200 but user is not in the list

Hi, I am hitting this api to add a user to an organization https://kinde.com/api/docs/#add-organization-users and I am getting a 200. I try to list the users for that Organization right after, I am getting a list of user but the one I have just added is not in the list. Everything happens in the same function so I know I am listing the same org code. Could there possibly be an issue with this endpoint?
16 Replies
Oli - Kinde
Oli - Kinde14mo ago
Hi @.Mass., We cannot recreate this issue, would you be willing to share your code where you perform this, so we can debug further?
_.Mass._
_.Mass._OP14mo ago
Ok, thanks I must be doing something wrong then. I can provide a code snippet later today.
Oli - Kinde
Oli - Kinde14mo ago
I can get my team to look into the code snippet first thing Monday.
_.Mass._
_.Mass._OP14mo ago
Sounds good, thank you 👍
async function addOrganizationAccount(orgCode, accountId, token) {

const payload = {
users: [
{
id: accountId,
},
],
}

const response = await fetch(`https://${this.env.AUTH_DOMAIN}.kinde.com/api/v1/organizations/${orgCode}/users`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
accept: 'application/json',
contentType: 'application/json',
},
body: JSON.stringify(payload),
})

if (response.status !== 200) {
throw new Error(`add account to organization failed with status ${response.status}`)
}

const listing = await fetch(`https://${this.env.AUTH_DOMAIN}.kinde.com/api/v1/organizations/${orgCode}/users`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
accept: 'application/json',
contentType: 'application/json',
},
})

if (listing.status !== 200) {
throw new Error(`listing failed with status ${listing.status}`)
}
return await listing.json()

}
async function addOrganizationAccount(orgCode, accountId, token) {

const payload = {
users: [
{
id: accountId,
},
],
}

const response = await fetch(`https://${this.env.AUTH_DOMAIN}.kinde.com/api/v1/organizations/${orgCode}/users`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
accept: 'application/json',
contentType: 'application/json',
},
body: JSON.stringify(payload),
})

if (response.status !== 200) {
throw new Error(`add account to organization failed with status ${response.status}`)
}

const listing = await fetch(`https://${this.env.AUTH_DOMAIN}.kinde.com/api/v1/organizations/${orgCode}/users`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
accept: 'application/json',
contentType: 'application/json',
},
})

if (listing.status !== 200) {
throw new Error(`listing failed with status ${listing.status}`)
}
return await listing.json()

}
here is the function, the first post returns a 200 so I expect the accountId to be in the list of the following listing, however the account is not there
_.Mass._
_.Mass._OP14mo ago
ok, the confusion is that the api docs states that a 200 ok means the user is added successfully, however I did a test with postman, I am getting a 200 with a message that says the user was not added
No description
Oli - Kinde
Oli - Kinde14mo ago
@.Mass. Would you mind sharing the body and URL you sent in postman? So we can try replicate this issue.
_.Mass._
_.Mass._OP14mo ago
sure no problem,
{
"users": [{"id":"kp:dbcf937949e6471c9794d198477b800f"}]
}
{
"users": [{"id":"kp:dbcf937949e6471c9794d198477b800f"}]
}
Oli - Kinde
Oli - Kinde14mo ago
Is the user already in the organization you are trying to add them to?
_.Mass._
_.Mass._OP14mo ago
ok yes, so I get that the second time I add the user to the same org. Which I guess it's fine to return the 200 it works with postman so there is definitely an issue in my code. I am happy to close this and resolve it on my end. Thank you for your help
Oli - Kinde
Oli - Kinde14mo ago
No worries, @.Mass. . Please don't hesitate to reach out for any further issues you have,
_.Mass._
_.Mass._OP14mo ago
this is very interesting. When I run the request via postman I get the User successfully added message the first time and No users added the following times. However with that code snippet I consistently get the No users added message. even when the user is actually not part of the org. I'll keep investigating
Oli - Kinde
Oli - Kinde14mo ago
Okay, if you get stuck please let me know.
_.Mass._
_.Mass._OP14mo ago
ok I think I found the issue was in my headers, I changed accept to Accept and contentType to Content-Type and the request is working as expected. I am getting the User successfully added message now.
Oli - Kinde
Oli - Kinde14mo ago
Ah good to know you fixed the issue by changing the headers. We will revisit the API docs to make sure this is more clear. Please don't hesitate to reach out again if you have any more issues.
_.Mass._
_.Mass._OP14mo ago
thank you
Oli - Kinde
Oli - Kinde14mo ago
No worries!
Want results from more Discord servers?
Add your server