# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.codemash.io/api/files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
