# Creating and Adding Values to \`custom\_fields\` via API

**URL:** https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477
**Category:** Support
**Created:** [July 30, 2018, 2:00pm UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477 "2018-07-30T14:00:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Curtis\_Kephart](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/curtis_kephart/32/89070_2.png) [@Curtis\_Kephart](https://meta.discourse.org/u/Curtis_Kephart)
#### Post date: [July 30, 2018, 2:00pm UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/1 "2018-07-30T14:00:58Z")

</div>

I am curious if there is any way to create and update Custom user fields (ie `users` \>\> `custom_field`) via the discourse API?

**Accessing `custon_fields`** - [Discourse’s docs show how to access `custom_fields` via `GET https://discourse.example.com/users/{username}.json`.](https://docs.discourse.org/#tag/Users%2Fpaths%2F~1users~1%7Busername%7D.json%2Fget).

**Updating `user_fields`** - This discussion [“API: cannot update user” walks through how to use the API to PUT/update fields like `name` and `user_fields` (user fields created at admin/customize/user\_fields)](https://meta.discourse.org/t/api-cannot-update-user/63876).

But for the life of me, I cannot construct a working curl example in which `custon_fields` is created or updated.

For example,

```plaintext
curl -X "PUT" "https://{myDomain}/users/{username}.json?api_key= ***&api_username=***" \
     -H "Content-Type: application/json" \
     -d $'{
  "custom_fields":{"test-val":"0.1"}
}`

```

Returns the following, which looks similar to a successful PUT (e.g. updating `name` or updating already-created `user_fields`).  
However, `custom_fields` is not updated.

```plaintext
...
> Accept: application/json, text/xml, application/xml, */*
-> 
<- HTTP/1.1 200 OK
<- Server: nginx
<- Date: Mon, 30 Jul 2018 13:57:02 GMT
<- Content-Type: application/json; charset=utf-8
<- Transfer-Encoding: chunked
<- Connection: keep-alive
<- Vary: Accept-Encoding
<- X-Frame-Options: SAMEORIGIN
<- X-XSS-Protection: 1; mode=block
<- X-Content-Type-Options: nosniff
<- X-Download-Options: noopen
<- X-Permitted-Cross-Domain-Policies: none
<- Referrer-Policy: strict-origin-when-cross-origin
<- X-Discourse-Username: EconomiCurtis
<- X-Discourse-Route: users/show
<- Cache-Control: no-store, must-revalidate, private, max-age=0
....

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [July 30, 2018, 11:58pm UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/2 "2018-07-30T23:58:45Z")

</div>

This it tricky, the field needs to be whitelisted can you add it to `/admin/customize/user_fields` or is this field meant to be completely hidden from users?

---

<div class="post-metadata">

### Author: ![Curtis\_Kephart](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/curtis_kephart/32/89070_2.png) [@Curtis\_Kephart](https://meta.discourse.org/u/Curtis_Kephart)
#### Post date: [July 31, 2018, 7:28am UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/3 "2018-07-31T07:28:02Z")

</div>

The desired field would need to be completely hidden to users. (The current use-case is a set of user-specific statistics calculated outside of discourse - those metrics will be used to rank-order users listed on a groups page)

I realize `user_fields` has options for a field the user would only see once, upon sign-up. Is the best course of action to create a user\_field and hide it on sign-up vis css?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [July 31, 2018, 7:39am UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/4 "2018-07-31T07:39:16Z")

</div>

> [@Curtis\_Kephart](#):
>
> Is the best course of action to create a user\_field and hide it on sign-up vis css?

Short of a plugin I think that is your only option here.

---

<div class="post-metadata">

### Author: ![jezra](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jezra/32/105986_2.png) [@jezra](https://meta.discourse.org/u/jezra)
#### Post date: [August 6, 2018, 11:33pm UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/5 "2018-08-06T23:33:03Z")

</div>

When updating the custom fields via the API, use the custom fields ‘number’ and not it’s name. If ‘test-val’ were the first field you created, the data line of your script would be:  
`-d '{ "user_fields": { "1": "0.1" } }'`

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 27, 2018, 10:41am UTC](https://meta.discourse.org/t/creating-and-adding-values-to-custom-fields-via-api/93477/6 "2018-11-27T10:41:36Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
