# Files

## Upload File

<mark style="color:green;">`POST`</mark> `https://api.codemash.io/:version/files`

Uploads a file.

#### Path Parameters

| Name    | Type   | Description                      |
| ------- | ------ | -------------------------------- |
| version | string | The version of the API endpoint. |

#### Headers

| Name           | Type   | Description                                            |
| -------------- | ------ | ------------------------------------------------------ |
| Authorization  | string | Your project's secret key.                             |
| x-cm-projectid | string | Your project's ID. Can be passed as a query parameter. |

#### Request Body

| Name | Type   | Description                |
| ---- | ------ | -------------------------- |
| path | string | Path in your file browser. |

{% tabs %}
{% tab title="200 Returns an ID of created notification group." %}

```javascript
{
    Result: ""
}
```

{% endtab %}

{% tab title="401 Returns if the user does not have a valid permission to call this method." %}

```
```

{% endtab %}

{% tab title="500 " %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title=".NET" %}

```csharp
var client = new CodeMashClient(apiKey, projectId);
var filesService = new CodeMashFilesService(client);

var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = $"{directory}\\test.txt";

using (var fsSource = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    var response = await filesService.UploadFileAsync(fsSource, "test.txt", 
        new UploadFileRequest
        {
            Path = "folder1"
        }
    );
}
```

{% endtab %}

{% tab title="Node" %}

```
```

{% endtab %}

{% tab title="PHP" %}

```php
use Codemash\CodemashClient;
use Codemash\CodemashFile;

class CodemashService
{
    protected CodemashFile $codemashFile;

    public function __construct()
    {
        $secretKey = '{YOUR_SECRET_KEY}';
        $projectId = '{YOUR_PROJECT_ID}';

        $client = new CodemashClient($secretKey, $projectId);
        $this->codemashFile = new CodemashFile($client);
    }

    public function uploadFile()
    {
        $responseData = $this->codemashFile->uploadFile([
            'path' => 'folder1',
            'fileUri' => '{YOUR_FILE_PATH}',
            'fileName' => '{YOUR_FILE_NAME}',
        ]);
    }
}
```

{% endtab %}
{% endtabs %}

## Upload Record File

<mark style="color:green;">`POST`</mark> `https://api.codemash.io/:version/db/:collectionName/files`

Uploads a file for a record field.

#### Path Parameters

| Name           | Type   | Description                      |
| -------------- | ------ | -------------------------------- |
| version        | string | The version of the API endpoint. |
| collectionName | string | Name of a collection.            |

#### Headers

| Name           | Type   | Description                                            |
| -------------- | ------ | ------------------------------------------------------ |
| Authorization  | string | Your project's secret key.                             |
| x-cm-projectid | string | Your project's ID. Can be passed as a query parameter. |

#### Request Body

| Name            | Type   | Description                           |
| --------------- | ------ | ------------------------------------- |
| recordId        | string | The ID of a record inside collection. |
| uniqueFieldName | string | Record file field name.               |

{% tabs %}
{% tab title="200 Returns an ID of created notification group." %}

```javascript
{
    Result: ""
}
```

{% endtab %}

{% tab title="401 Returns if the user does not have a valid permission to call this method." %}

```
```

{% endtab %}

{% tab title="500 " %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title=".NET" %}

```csharp
var client = new CodeMashClient(apiKey, projectId);
var filesService = new CodeMashFilesService(client);

var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = $"{directory}\\test.txt";

using (var fsSource = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    var response = await filesService.UploadRecordFileAsync(fsSource, "test.txt", 
        new UploadRecordFileRequest
        {
            RecordId = "{RECORD_ID}",
            UniqueFieldName = "file_field_name"
        }
    );
}
```

{% endtab %}

{% tab title="Node" %}

```
```

{% endtab %}

{% tab title="PHP" %}

```php
use Codemash\CodemashClient;
use Codemash\CodemashFile;

class CodemashService
{
    protected CodemashFile $codemashFile;

    public function __construct()
    {
        $secretKey = '{YOUR_SECRET_KEY}';
        $projectId = '{YOUR_PROJECT_ID}';

        $client = new CodemashClient($secretKey, $projectId);
        $this->codemashFile = new CodemashFile($client);
    }

    public function uploadRecordFile()
    {
        $responseData = $this->codemashFile->uploadRecordFile([
            'fileUri' => '{YOUR_FILE_PATH}',
            'fileName' => '{YOUR_FILE_NAME}',
            'collectionName' => '{COLLECTION_NAME}',
            'recordId' => '{RECORD_ID}',
            'uniqueFieldName' => 'file_field_name',
        ]);
    }
}
```

{% endtab %}
{% endtabs %}

## Upload User File

<mark style="color:green;">`POST`</mark> `https://api.codemash.io/:version/membership/users/files`

Uploads a file for a user meta field.

#### Path Parameters

| Name    | Type   | Description                      |
| ------- | ------ | -------------------------------- |
| version | string | The version of the API endpoint. |

#### Headers

| Name           | Type   | Description                                            |
| -------------- | ------ | ------------------------------------------------------ |
| Authorization  | string | Your project's secret key.                             |
| x-cm-projectid | string | Your project's ID. Can be passed as a query parameter. |

#### Request Body

| Name          | Type   | Description                |
| ------------- | ------ | -------------------------- |
| userId        | string | The ID of a user.          |
| metaFieldName | string | User meta-file field name. |

{% tabs %}
{% tab title="200 Returns an ID of created notification group." %}

```javascript
{
    Result: ""
}
```

{% endtab %}

{% tab title="401 Returns if the user does not have a valid permission to call this method." %}

```
```

{% endtab %}

{% tab title="500 " %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title=".NET" %}

```csharp
var client = new CodeMashClient(apiKey, projectId);
var filesService = new CodeMashFilesService(client);

var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = $"{directory}\\test.txt";

using (var fsSource = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    var response = await filesService.UploadUserFileAsync(fsSource, "test.txt", 
        new UploadUserFileRequest
        {
            UserId = "{USER_ID}",
            MetaFieldName = "file_field_name"
        }
    );
}
```

{% endtab %}

{% tab title="Node" %}

```
```

{% endtab %}

{% tab title="PHP" %}

```php
use Codemash\CodemashClient;
use Codemash\CodemashFile;

class CodemashService
{
    protected CodemashFile $codemashFile;

    public function __construct()
    {
        $secretKey = '{YOUR_SECRET_KEY}';
        $projectId = '{YOUR_PROJECT_ID}';

        $client = new CodemashClient($secretKey, $projectId);
        $this->codemashFile = new CodemashFile($client);
    }

    public function uploadUserFile()
    {
        $responseData = $this->codemashFile->uploadUserFile([
            'fileUri' => '{YOUR_FILE_PATH}',
            'fileName' => '{YOUR_FILE_NAME}',
            'userId' => '{USER_ID}',
            'metaFieldName' => 'file_field_name',
        ]);
    }
}
```

{% endtab %}
{% endtabs %}
