How to change user fields with API

(Edit: see 2nd post for solution: how to change user fields with API)

@sam, you mentioned (in a post in a topic now closed) with regards to someone who wanted to use the API to update a custom field in a user record: “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?”

Two quick questions

  1. What is /admin/customize/ endpoint?
  2. How do you “whitelist” a field? (What does that mean-- whitelist it so the API can access it, somehow?!)

I’ve searched for the answers for both of these without luck, hopefully you (or someone else who knows) can help.

BTW my ultimate goal seems simple: I want to be able to set/get the value of a custom user field with the API. Yet I’m finding it’s incredibly difficult to get a clear answer about how to do this. :frowning:

So far I’ve figured out:

  1. You can sorta pass custom fields via SSO, but that’s not the API
  2. The custom fields appear to be referenced only by number, not by name, when the front-end calls the API (via watching the XHR request) - obviously that makes a big difference if I’m looking for it by name but it’s really users[2]

As you can see, it’s non-obvious :wink: and I figure the ‘get’ is easier since it will be part of the user record (which I can get now with the API), but I can’t confirm until I can ‘set’ it with the API.

Anyhow… once I finally figure this out, how can I contribute that knowledge back in a way that will help the next person trying to do this?

إعجابَين (2)

You can create a user with custom fields via the API by passing in user_fields[1] like this:

curl -i -sS -X POST "http://localhost:3000/users"  \
-H "Content-Type: multipart/form-data;"  \
-H "Api-Key: d19b5a38ed13745f4c5..."  \
-H "Api-Username: system"  \
-F "name=cbbc7188c201b59ebc06"  \
-F "username=cbbc7188c201b59ebc06"  \
-F "email=cbbc7188c201b59ebc06@example.com"  \
-F "password=b6629ed30b4bc3de32ce452d171540b0"  \
-F "active=true"  \
-F "approved=true"  \
-F "user_fields[1]=a874d830b60"  \
-F "user_fields[2]=732418b131b"

image

And you can update them by sending a PUT request to /u/<username>.json

7 إعجابات

/admin/customize/user_fields is a UI page where the custom user fields are configured.

3 إعجابات

Thank you for all of this help! FWIW I’d love to see the ability to refer to user_fields by their name (seems much less brittle?) but this explanation and info you’ve provided me is enormously helpful. I appreciate it :slight_smile:

إعجاب واحد (1)

هل تستخدم نفس نقطة النهاية POST لـ تحديث مستخدم؟ هل جميع الحقول مطلوبة؟

إعجابَين (2)

مرحباً! أريد أن أعرف كيف أحصل على فهرس user_fields؟ هل يجب أن أذهب إلى وحدة تحكم الويب وأحصل على الفهرس؟ هل هناك واجهة برمجة تطبيقات للحصول على user_fields؟ عندها يمكنني الحصول على الفهرس. شكراً.

إعجاب واحد (1)

يمكنك إجراء طلب GET إلى /admin/customize/user_fields وسيقوم بإرجاع قائمة بحقول المستخدم:

{
  "user_fields": [
    {
      "id": 2,
      "name": "Favorite OS",
      "description": "What is your favorite Operating System?",
      "field_type": "text",
      "editable": true,
      "required": false,
      "show_on_profile": true,
      "show_on_user_card": true,
      "searchable": false,
      "position": 1
    }
  ]
}
5 إعجابات

شكرا لك. إجابتك مفيدة جدا لي!

إعجابَين (2)