# User API - not able to add users

**URL:** <https://forum.kintone.dev/t/user-api-not-able-to-add-users/837>\
**Category:** General\
**Tags:** user-api, c-sharp, add-user-api, user-settings\
**Created:** [August 17, 2023, 8:09pm UTC](https://forum.kintone.dev/t/user-api-not-able-to-add-users/837 "2023-08-17T20:09:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![djlewis](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@djlewis](https://forum.kintone.dev/u/djlewis)\
**Post date:** [August 17, 2023, 8:09pm UTC](https://forum.kintone.dev/t/user-api-not-able-to-add-users/837/1 "2023-08-17T20:09:45Z")

</div>

I am trying to use the User API to add users.  
I have used it successfully for a long time, but it no longer works recently. I am wondering if something changed.

I am posting my code below.  
Could you please review it to see if I am doing anything wrong?  
Thank you.

```csharp
var AddUsers = vaiUsers.Where(p => !kintoneUsers.Any(p2 => p2.employeeNumber == p.WdEmployeeId)).ToList();
var SUBDOMAIN = "example_subdomain";

List<object> NewUsers = new List<object>();

foreach (var item in AddUsers)
{
  NewUsers.Add(new
  {
    code = item.AdUserId.Trim().ToLower(),
    valid = true,
    password = newKintoneUserPassword,
    name = item.FirstName + " " + item.LastName,
    surName = item.LastName,
    givenName = item.FirstName,
    localNameLocale = "en",
    timezone = "America/New_York",
    locale = "en",
    email = item.EmailAddress.ToLower(),
    employeeNumber = item.WdEmployeeId,
    customItemValues = new List<CustomItemValue> { new CustomItemValue { code = "supervisor", value = (from k in kintoneUsers where k.employeeNumber == item.WdManagerId01 select k.id).FirstOrDefault() } }
  });

  string jsonUpdateUsers = "{\"users\":" + JsonConvert.SerializeObject(splitAddUserInKintone) + "}";

  using (var httpClient = new HttpClient())
  {
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://{SUBDOMAIN}.kintone.com/v1/users.json"))
    {
      byte[] toBeEncoded = System.Text.ASCIIEncoding.ASCII.GetBytes("apiUser:" + apiUserPassword);

      var encoded2 = System.Convert.ToBase64String(toBeEncoded);

      request.Headers.TryAddWithoutValidation("X-Cybozu-Authorization", encoded2);

      request.Content = new StringContent(jsonUpdateUsers);
      request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

      var response = await httpClient.SendAsync(request);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Genji](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/genji/32/646_2.png) [@Genji](https://forum.kintone.dev/u/Genji)\
**Post date:** [August 18, 2023, 5:50am UTC](https://forum.kintone.dev/t/user-api-not-able-to-add-users/837/2 "2023-08-18T05:50:07Z")

</div>

Hello @djlewis

Welcome to the Kintone Developer Forum & thank you for posting your question!

## Housekeeping

To better help you out, please provide more information regarding the following:

- Your current situation
- Your desired outcome
- What you have tried so far
- Context to your code (language, framework, running environment, etc.)
- Error message you got (Screenshots are helpful 👌)

Additional questions specific to your code:

- What is in the `splitAddUserInKintone` variable?
- What is the data format from the `WdEmployeeId` variable?

Also, we highly recommend you use our question template below to help provide vital information for our community to help you out.

Lastly, I removed your hard-coded subdomain from your post for security reasons and replaced it with the `SUBDOMAIN` variable.

### Question Template

```markdown

## Question / Problem
_Got an error message?_
_Building a JS customization?_

## Current Situation
_How is your Kintone App configured?_
_What are you trying to do?_

### Code / Attempts
_Share your code and setup_

### Error Message
_Screenshots are helpful_

## Desired Outcome / Expected Behavior
_Screenshots are helpful_

## Referenced Resources
_List the [tutorials](https://kintone.dev/en/tutorials/), [API docs](https://kintone.dev/en/docs/), & [help article](https://get.kintone.help/en/) you read_

```

## Recent Changes to User API?

The most "recent" changes made to the Kintone User API were back in [May 2022](https://kintone.dev/en/news/api-updates/2022-05/).

The most recent changes to the [Add Users API](https://kintone.dev/en/docs/common/user-api/users/add-users/) especially was back in [March 2021](https://kintone.dev/en/news/api-updates/2021-03/).

## Using Add Users API

I can confirm that the Add Users API is still working as expected. I was able to add a user to my Kintone App using the following API calls:

### 1. Add User

Add a 'test' user to my Kintone subdomain using the [Add Users API](https://kintone.dev/en/docs/common/user-api/users/add-users/).

```js
const body = { users: [{ code: 'test', valid: true, password: 'password', name: 'test' }] };
kintone.api( kintone.api.url('/v1/users', true), 'POST', body,
  function (resp) { console.log(resp); },
  function (error) { console.log(error); }
);

```

### 2. Update User Services

Note that once a user is added with this API, the [Update User Services API](https://kintone.dev/en/docs/common/user-api/users/update-user-services/) needs to be used against this user for the user to have access to Kintone services.

```js
const body = { users: [{ code: 'test', services: ['kintone'] } ] };
kintone.api( kintone.api.url('/v1/users/services', true), 'PUT', body,
  function (resp) { console.log(resp); },
  function (error) { console.log(error); }
);

```

### 3. Get User Services

To confirm that the user has been added to the Kintone subdomain, use the [Get User Services API](https://kintone.dev/en/docs/common/user-api/users/get-user-services/).

```js
const body = { 'codes': ['test'] };
kintone.api(kintone.api.url('/v1/users/services', true), 'GET', body,
  function(resp) { console.log(resp.users); },
  function(error) { console.log(error); }
);

```

## Common Errors

Here are some common errors that may be relevant:

**Authentication**

- Verify the `apiUser` and `apiUserPassword` values.
- Verify that the Basic Authentication header is correct.

**JSON Serialization**

- Verify that the serialization process (`JsonConvert.SerializeObject`) works correctly.

**Data Compatibility**

- Verify that the data types and structures used in the code match the actual data you are working with.
