# Create a user

`POST /api/v1/users`

Authentication (security requirement alternatives):

```json
[
  {
    "OAuth2": [
      "admin"
    ]
  }
]
```

- OAuth2: oauth2. Authenticating via OAuth requires the following steps:
- Have an activated account
- [Generate] an access token for that account at `/api/v1/users/token`.
- Make requests with the *Authorization: Bearer <token\>* header
- Profit, depending on the role assigned to the account

Note that the __access token is valid for 1 day__ and is given
along with a __refresh token valid for 2 weeks__.

[Generate]: https://docs.joinpeertube.org/api/rest-getting-started
; scopes: admin

Request body: application/json (required)

If the smtp server is configured, you can leave the password empty and an email will be sent
asking the user to set it first.


Type: AddUser

Schema: [AddUser](https://sourcey-peertube.pages.dev/api-rest-reference/models/adduser.md)

- `username` (required)
  Reference: [username](https://sourcey-peertube.pages.dev/api-rest-reference/models/username.md); Type: username
  
  immutable name of the user, used to find or mention its actor
  
  minLength: 1
  
  maxLength: 50
  
  pattern: "/^[a-z0-9._]+$/"
  
  Example:
  
  ```json
  "chocobozzz"
  ```

- `password` (required)
  Reference: [password](https://sourcey-peertube.pages.dev/api-rest-reference/models/password.md); Type: password, password
  
  minLength: 6
  
  maxLength: 50

- `email` (required)
  Type: string, email
  
  The user email

- `videoQuota` (optional)
  Type: integer
  
  The user video quota in bytes
  
  Example:
  
  ```json
  -1
  ```

- `videoQuotaDaily` (optional)
  Type: integer
  
  The user daily video quota in bytes
  
  Example:
  
  ```json
  -1
  ```

- `channelName` (optional)
  Reference: [usernameChannel](https://sourcey-peertube.pages.dev/api-rest-reference/models/usernamechannel.md); Type: usernameChannel
  
  immutable name of the channel, used to interact with its actor
  
  minLength: 1
  
  maxLength: 50
  
  pattern: "/^[a-zA-Z0-9\\\\-_.:]+$/"
  
  Example:
  
  ```json
  "framasoft_videos"
  ```

- `role` (required)
  Reference: [UserRole](https://sourcey-peertube.pages.dev/api-rest-reference/models/userrole.md); Type: UserRole
  
  The user role (Admin = `0`, Moderator = `1`, User = `2`)
  
  enum: [0,1,2]
  
  Example:
  
  ```json
  2
  ```

- `adminFlags` (optional)
  Reference: [UserAdminFlags](https://sourcey-peertube.pages.dev/api-rest-reference/models/useradminflags.md); Type: UserAdminFlags
  
  Admin flags for the user (None = `0`, Bypass video blocklist = `1`)
  
  enum: [0,1]
  
  Example:
  
  ```json
  1
  ```

- `currentPassword` (optional)
  Type: currentPassword
  
  Required to create an administrator, see [password confirmation](#section/Password-confirmation)
  
  allOf:
    Reference: [currentPassword](https://sourcey-peertube.pages.dev/api-rest-reference/models/currentpassword.md); Type: currentPassword, password
    
    Password of the authenticated user, required if [password confirmation](#section/Password-confirmation) is enabled

Responses:

- 200: user created

Content type: application/json

Type: AddUserResponse

Schema: [AddUserResponse](https://sourcey-peertube.pages.dev/api-rest-reference/models/adduserresponse.md)

- `user` (optional)
  Type: object
  
  - `id` (optional)
    Reference: [id](https://sourcey-peertube.pages.dev/api-rest-reference/models/id.md); Type: id
    
    minimum: 1
    
    Example:
    
    ```json
    42
    ```
  
  - `account` (optional)
    Type: object
    
    - `id` (optional)
      Reference: [id](https://sourcey-peertube.pages.dev/api-rest-reference/models/id.md); Type: id
      
      minimum: 1
      
      Example:
      
      ```json
      42
      ```

- 403: insufficient authority to create an admin or moderator

## Examples

### cURL

```bash
curl -X POST 'https://peertube2.cpy.re/api/v1/users' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "username": "chocobozzz",
  "password": "********",
  "email": "user@example.com",
  "videoQuota": -1,
  "videoQuotaDaily": -1,
  "channelName": "framasoft_videos",
  "role": 2,
  "adminFlags": 1
}'
```

### JavaScript

```javascript
const body = `{
  "username": "chocobozzz",
  "password": "********",
  "email": "user@example.com",
  "videoQuota": -1,
  "videoQuotaDaily": -1,
  "channelName": "framasoft_videos",
  "role": 2,
  "adminFlags": 1
}`;

const response = await fetch('https://peertube2.cpy.re/api/v1/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "username": "chocobozzz",
  "password": "********",
  "email": "user@example.com",
  "videoQuota": -1,
  "videoQuotaDaily": -1,
  "channelName": "framasoft_videos",
  "role": 2,
  "adminFlags": 1
}'''

response = requests.post(
    'https://peertube2.cpy.re/api/v1/users',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Bearer YOUR_TOKEN',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```
