how to create a user under a tenant using go sdk e...
# permit-saas
p
how to create a user under a tenant using go sdk example i have a tenant created with abc key and from go sdk i am creating a user i am sending the tenant_id using the create method the user is getting created but its not assosiated to the tenant i am not able to see that user under that tenant on the UI i have to go on the UI and set the tenant
a
Hi @Pratham Khodwe, I'm an AI assistant for Permit.io. I'll analyze available sources and get back to you shortly.
p
Hey, thanks for reaching out ! It seems our team is a little busy right now, a human will be with you within the next hour or so. In the meantime, can I help you ?
a
Based on the documentation, when creating a user in Go SDK, you need to assign them to a tenant with a role. Simply creating a user doesn't automatically associate them with a tenant - you must also assign a role to establish that relationship. (1) Here's the correct approach: Option 1: Create user with role assignment during creation
Copy code
userData := models.UserCreate{
    Key: "user|123456789",
    Email: "<mailto:john@smith.com|john@smith.com>",
    FirstName: "John",
    LastName: "Smith",
    RoleAssignments: []models.RoleAssignmentCreate{
        {
            Role: "Manager",
            Tenant: "abc", // Your tenant key
        },
    },
}
Option 2: Create user first, then assign role First create the user:
Copy code
user := enforcement.UserBuilder("<mailto:john@doe.com|john@doe.com>") .Build()
(2) Then assign them to the tenant with a role using the API. The role assignment is what creates the tenant association. (1) Important Note The documentation shows that users appear in the Directory under "All Tenants" when first creat
To learn more, see the sources I considered: [[1] Sync your First User](https://docs.permit.io/overview/sync-your-first-user-with-sdk) [[2] Golang Quickstart](https://docs.permit.io/sdk/golang/quickstart-golang)
n
Hi @Pratham Khodwe - thanks for reaching out 🙂 the AI agent did a good job explaining how to assign the user to a tenant with role assignment during creation - let me know if you need any further clarification.
✅ 1