# Adding metadata to user using management API

**URL:** <https://community.auth0.com/t/adding-metadata-to-user-using-management-api/84693>\
**Category:** Get Help\
**Tags:** signup, php, auth0\
**Created:** [June 7, 2022, 7:43am UTC](https://community.auth0.com/t/adding-metadata-to-user-using-management-api/84693 "2022-06-07T07:43:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bird](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bird/32/37850_2.png) [@bird](https://community.auth0.com/u/bird)\
**Post date:** [June 7, 2022, 7:43am UTC](https://community.auth0.com/t/adding-metadata-to-user-using-management-api/84693/1 "2022-06-07T07:43:01Z")

</div>

I am currently having a slight issue with registering users via the API and I think it’s down to our Pre-User Registration flow.

We currently have a Pre-User Registration flow that looks like this:

```auto
/**
 * @param {Event} event - Details about registration event.
 * @param {PreUserRegistrationAPI} api
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  api.user.setUserMetadata("lang", event.transaction.locale)
}

```

This flow is supposed to add the user lang to the metadata so that we can use it for whatever we need.

My code to create a new user looks like this:

```auto
return $this->managementService->users()->create([
    'connection' => $this->connectionId,
    'email' => $user->getEmail(),
    'password' => substr(str_shuffle($permittedChars), 0, 16)
]);

```

When I add a user, then I get an error in the Auth0 logs that says:

```auto
TypeError on pre-user-registration: Cannot read property 'locale' of undefined

```

It makes sense, to me, that the flow is trying to add the lang, but because `event.transaction` is undefined, it fails. I’ve tried adding fields to my `create` body, but then it fails validation.

The one solution I see is to make a fallback for lang in the `onExecutePreUserRegistration` flow, but is there another way to do this in my PHP code?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [June 16, 2022, 5:43pm UTC](https://community.auth0.com/t/adding-metadata-to-user-using-management-api/84693/3 "2022-06-16T17:43:27Z")

</div>

Hi @bird,

Welcome to the Auth0 Community!

You can handle this case with a conditional in your Action and add the metadata directly via the management API.

For example:

```javascript
/**
 * @param {Event} event - Details about registration event.
 * @param {PreUserRegistrationAPI} api
 */
exports.onExecutePreUserRegistration = async (event, api) => {
  if (event.transaction) {
    api.user.setUserMetadata("lang", event.transaction.locale)
  }
}

```

And when you register a user via the management API you can set `"user_metadata":{"lang":"YOUR_LANG"}` directly. It looks like you should be able to pass it in the body of the request: [Auth0-PHP](https://auth0.github.io/auth0-PHP/classes/Auth0-SDK-Contract-API-Management-UsersInterface.html#method_create).

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 5:12pm UTC](https://community.auth0.com/t/adding-metadata-to-user-using-management-api/84693/4 "2022-12-21T17:12:16Z")

</div>


