1"use strict";(globalThis.webpackChunkmy_website||=[]).push([[4210],{19739(e,s,r){r.r(s),r.d(s,{assets:()=>h,contentTitle:()=>c,default:()=>a,frontMatter:()=>i,metadata:()=>t,toc:()=>l});const t=JSON.parse('{"id":"sdk/golang/user/Get","title":"Get a user with the Go SDK","description":"Reference for Permit.Api.Users.Get(), GetByKey(), and GetById() in the Go SDK, which fetch one user by key or ID.","source":"@site/docs/sdk/golang/user/Get.mdx","sourceDirName":"sdk/golang/user","slug":"/sdk/golang/user/Get","permalink":"/sdk/golang/user/Get","draft":false,"unlisted":false,"editUrl":"https://github.com/permitio/docs/edit/master/docs/sdk/golang/user/Get.mdx","tags":[],"version":"current","frontMatter":{"title":"Get a user with the Go SDK","sidebar_label":"Get","description":"Reference for Permit.Api.Users.Get(), GetByKey(), and GetById() in the Go SDK, which fetch one user by key or ID."},"sidebar":"sdks","previous":{"title":"Delete","permalink":"/sdk/golang/user/Delete"},"next":{"title":"GetAssignedRoles","permalink":"/sdk/golang/user/GetAssignedRoles"}}');var d=r(74848),n=r(28453);const i={title:"Get a user with the Go SDK",sidebar_label:"Get",description:"Reference for Permit.Api.Users.Get(), GetByKey(), and GetById() in the Go SDK, which fetch one user by key or ID."},c=void 0,h={},l=[{value:"Get a user by key with <code>Users.Get</code> or <code>Users.GetByKey</code>",id:"get--getbykey",level:2},{value:"<code>Users.Get</code> parameters",id:"parameters",level:3},{value:"Example: get a user by key",id:"implementation",level:3},{value:"Get a user by ID with <code>Users.GetById</code>",id:"getbyid",level:2},{value:"<code>Users.GetById</code> parameters",id:"parameters-1",level:3},{value:"Example: get a user by ID",id:"implementation-1",level:3},{value:"User return value and errors",id:"user-return-value-and-errors",level:2},{value:"Related user methods",id:"related-user-methods",level:2}];function o(e){const s={a:"a",code:"code",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,n.R)(),...e.components};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsxs)(s.p,{children:[(0,d.jsx)(s.code,{children:"Permit.Api.Users.Get()"})," fetches one user and its details from the environment that your API key belongs to. This reference is for Go developers who read user data from backend code. Use ",(0,d.jsx)(s.code,{children:"Get"})," or ",(0,d.jsx)(s.code,{children:"GetByKey"})," when you have the user key, and ",(0,d.jsx)(s.code,{children:"GetById"})," when you have the user ID (a UUID)."]}),"\n",(0,d.jsxs)(s.h2,{id:"get--getbykey",children:["Get a user by key with ",(0,d.jsx)(s.code,{children:"Users.Get"})," or ",(0,d.jsx)(s.code,{children:"Users.GetByKey"})]}),"\n",(0,d.jsxs)(s.p,{children:[(0,d.jsx)(s.code,{children:"Users.GetByKey"})," calls ",(0,d.jsx)(s.code,{children:"Users.Get"})," with the same arguments, so the two methods behave the same."]}),"\n",(0,d.jsx)(s.p,{children:(0,d.jsx)(s.code,{children:"func (u *Users) Get(ctx context.Context, userKey string) (*models.UserRead, error)"})}),"\n",(0,d.jsx)(s.p,{children:(0,d.jsx)(s.code,{children:"func (u *Users) GetByKey(ctx context.Context, userKey string) (*models.UserRead, error)"})}),"\n",(0,d.jsxs)(s.h3,{id:"parameters",children:[(0,d.jsx)(s.code,{children:"Users.Get"})," parameters"]}),"\n",(0,d.jsxs)(s.table,{children:[(0,d.jsx)(s.thead,{children:(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.th,{children:"Parameter"}),(0,d.jsx)(s.th,{children:"Type"}),(0,d.jsx)(s.th,{children:"Required"}),(0,d.jsx)(s.th,{children:"Description"})]})}),(0,d.jsxs)(s.tbody,{children:[(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"ctx"})}),(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"context.Context"})}),(0,d.jsx)(s.td,{children:"Yes"}),(0,d.jsx)(s.td,{children:"The context of the request."})]}),(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"userKey"})}),(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"string"})}),(0,d.jsx)(s.td,{children:"Yes"}),(0,d.jsx)(s.td,{children:"The key of the user."})]})]})]}),"\n",(0,d.jsx)(s.h3,{id:"implementation",children:"Example: get a user by key"}),"\n",(0,d.jsxs)(s.p,{children:["The example uses a client named ",(0,d.jsx)(s.code,{children:"Permit"}),", created with ",(0,d.jsx)(s.code,{children:"permit.NewPermit()"})," as shown in ",(0,d.jsx)(s.a,{href:"/sdk/golang/quickstart-golang",children:"Check permissions with the Go SDK"}),", and a ",(0,d.jsx)(s.code,{children:"ctx"})," of type ",(0,d.jsx)(s.code,{children:"context.Context"}),". Replace ",(0,d.jsx)(s.code,{children:"user-key"})," with the key of your user:"]}),"\n",(0,d.jsx)(s.pre,{children:(0,d.jsx)(s.code,{className:"language-go",children:'user, err := Permit.Api.Users.Get(ctx, "user-key")\n'})}),"\n",(0,d.jsxs)(s.h2,{id:"getbyid",children:["Get a user by ID with ",(0,d.jsx)(s.code,{children:"Users.GetById"})]}),"\n",(0,d.jsxs)(s.p,{children:[(0,d.jsx)(s.code,{children:"Users.GetById"})," takes the user ID as a ",(0,d.jsx)(s.code,{children:"uuid.UUID"})," from the ",(0,d.jsx)(s.code,{children:"github.com/google/uuid"})," package, converts it to a string, and calls ",(0,d.jsx)(s.code,{children:"Users.Get"}),"."]}),"\n",(0,d.jsx)(s.p,{children:(0,d.jsx)(s.code,{children:"func (u *Users) GetById(ctx context.Context, userId uuid.UUID) (*models.UserRead, error)"})}),"\n",(0,d.jsxs)(s.h3,{id:"parameters-1",children:[(0,d.jsx)(s.code,{children:"Users.GetById"})," parameters"]}),"\n",(0,d.jsxs)(s.table,{children:[(0,d.jsx)(s.thead,{children:(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.th,{children:"Parameter"}),(0,d.jsx)(s.th,{children:"Type"}),(0,d.jsx)(s.th,{children:"Required"}),(0,d.jsx)(s.th,{children:"Description"})]})}),(0,d.jsxs)(s.tbody,{children:[(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"ctx"})}),(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"context.Context"})}),(0,d.jsx)(s.td,{children:"Yes"}),(0,d.jsx)(s.td,{children:"The context of the request."})]}),(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"userId"})}),(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"uuid.UUID"})}),(0,d.jsx)(s.td,{children:"Yes"}),(0,d.jsx)(s.td,{children:"The ID that Permit assigned to the user. This is not the user key."})]})]})]}),"\n",(0,d.jsx)(s.h3,{id:"implementation-1",children:"Example: get a user by ID"}),"\n",(0,d.jsxs)(s.p,{children:["Call ",(0,d.jsx)(s.code,{children:"Users.GetById"})," and pass the user ID as a ",(0,d.jsx)(s.code,{children:"uuid.UUID"})," value. Parse an ID string with ",(0,d.jsx)(s.code,{children:"uuid.MustParse"}),":"]}),"\n",(0,d.jsx)(s.pre,{children:(0,d.jsx)(s.code,{className:"language-go",children:'userId := uuid.MustParse("<user-id>")\nuser, err := Permit.Api.Users.GetById(ctx, userId)\n'})}),"\n",(0,d.jsx)(s.h2,{id:"user-return-value-and-errors",children:"User return value and errors"}),"\n",(0,d.jsxs)(s.p,{children:["On success, each method returns a ",(0,d.jsx)(s.code,{children:"*models.UserRead"})," with the user's ",(0,d.jsx)(s.code,{children:"Key"}),", ",(0,d.jsx)(s.code,{children:"Id"}),", ",(0,d.jsx)(s.code,{children:"Email"}),", ",(0,d.jsx)(s.code,{children:"FirstName"}),", ",(0,d.jsx)(s.code,{children:"LastName"}),", and ",(0,d.jsx)(s.code,{children:"Attributes"}),"."]}),"\n",(0,d.jsxs)(s.p,{children:["If the call fails, ",(0,d.jsx)(s.code,{children:"err"})," holds an ",(0,d.jsx)(s.code,{children:"errors.PermitError"})," from the ",(0,d.jsx)(s.code,{children:"github.com/permitio/permit-golang/pkg/errors"})," package. Its ",(0,d.jsx)(s.code,{children:"StatusCode"})," field has the HTTP status, and its ",(0,d.jsx)(s.code,{children:"ErrorCode"})," field has one of these codes:"]}),"\n",(0,d.jsxs)(s.table,{children:[(0,d.jsx)(s.thead,{children:(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.th,{children:(0,d.jsx)(s.code,{children:"ErrorCode"})}),(0,d.jsx)(s.th,{children:"Cause"})]})}),(0,d.jsxs)(s.tbody,{children:[(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"NotFound"})}),(0,d.jsx)(s.td,{children:"HTTP 404: no user with that key or ID exists in the environment."})]}),(0,d.jsxs)(s.tr,{children:[(0,d.jsxs)(s.td,{children:[(0,d.jsx)(s.code,{children:"Unauthorized"}),", ",(0,d.jsx)(s.code,{children:"ForbiddenAccess"})]}),(0,d.jsx)(s.td,{children:"HTTP 401 or 403: the API key is invali
1d or has no access to the environment."})]}),(0,d.jsxs)(s.tr,{children:[(0,d.jsx)(s.td,{children:(0,d.jsx)(s.code,{children:"UnexpectedError"})}),(0,d.jsx)(s.td,{children:"A server error (HTTP 5xx) or a network error."})]})]})]}),"\n",(0,d.jsx)(s.h2,{id:"related-user-methods",children:"Related user methods"}),"\n",(0,d.jsxs)(s.ul,{children:["\n",(0,d.jsx)(s.li,{children:(0,d.jsx)(s.a,{href:"/sdk/golang/user/GetAssignedRoles",children:"Get the roles assigned to a user with the Go SDK"})}),"\n",(0,d.jsx)(s.li,{children:(0,d.jsx)(s.a,{href:"/sdk/golang/user/SyncUser",children:"Sync a user with the Go SDK"})}),"\n",(0,d.jsx)(s.li,{children:(0,d.jsx)(s.a,{href:"/sdk/golang/user/Delete",children:"Delete a user with the Go SDK"})}),"\n"]})]})}function a(e={}){const{wrapper:s}={...(0,n.R)(),...e.components};return s?(0,d.jsx)(s,{...e,children:(0,d.jsx)(o,{...e})}):o(e)}},28453(e,s,r){r.d(s,{R:()=>i,x:()=>c});var t=r(96540);const d={},n=t.createContext(d);function i(e){const s=t.useContext(n);return t.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function c(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(d):e.components||d:i(e.components),t.createElement(n.Provider,{value:s},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.