users.create() returns Zendesk Error (422): Unprocessable Entity
Describe the Bug Create user returns Zendesk Error (422): Unprocessable Entity
Example Code
// Client
export function getZendeskUtilClient(): ZendeskClient {
const clientOptions: ZendeskClientOptions = {
token: process.env.ZENDESK_SANDBOX_TOKEN,
oauth: false,
subdomain: 'sandbox1718988640',
username: '[email protected]',
apiType: ['core'],
};
return createClient(clientOptions)
}
async function createUser() {
const zendesk = client;
try {
const response = await zendesk.users.create({ name:'Test TestUser', email: '[email protected]' , role: 'end-user'});
console.dir(response, {depth: null});
} catch (error) {
console.error(`ERROR in CREATE: ${error}`);
}
}
// Without Role
async function createUser() {
const zendesk = client;
try {
const response = await zendesk.users.create({ name:'Test TestUser', email: '[email protected]'});
console.dir(response, {depth: null});
} catch (error) {
console.error(`ERROR in CREATE: ${error}`);
}
}
// My usage above doesn't seem to be different from the example in the JSDoc
Creates a new user.
@param user — The user details.
@returns — The created user's details.
@async
@see — [https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html)
@example
const newUser = await client.users.create({name: 'John Doe', email: '[email protected]'});
Expected Behavior Create the user.. Actual Behavior ERROR in CREATE: Error: Request processing failed: Zendesk Error (422): Unprocessable Entity
Environment Information
-
node-zendeskversion: ^5.0.9 - Node.js version: v21.1.0
- Operating System: MacOS
- Any other relevant software versions? Nah
Additional Context
I'll update when I find out what the problem is. However, it also spits out this giant js blob along with the Zendesk response:
This is just a bug in the JSDoc usage or handling of the object parameter: It actually works with an Object called 'user' with those properties:
async function createUser() {
const zendesk = client;
let user = {
"user": {
"name": "John Doe",
"email": "[email protected]"
}
};
try {
const response = await zendesk.users.create(user);
console.dir(response, {depth: null});
} catch (error) {
console.error(`ERROR in CREATE: ${error}`);
}
}
EZ Fix - Might be more methods like this
Since this method also returns the default API Response object.. It will mess up all the type assertions. To work around this, you basically can just create your own:
interface ZendeskAPIUserResponse {
response: {
json: Function;
status: number;
headers: {
get: Function;
};
statusText: string;
};
result: User; // Imported the node-zendesk User type
}
async function handleCreateUserResponse(createUser: { user: { name: string; email: string } }) {
const zendesk = client;
try {
// Here, we have to cast the response to the correct type after unknown conversion
const response = await zendesk.users.create(createUser) as unknown as ZendeskAPIUserResponse;
const newUser = response.result; // Now this should correctly reference the user details
console.log('New User Created:', newUser);
} catch (error) {
console.error('Error creating user:', error);
}
}