# Bloq

## Creating a new bloq

We use chunking which is useful in big size files uploading. In chunk uploading, you can configure how much size each chunk should upload to the server.

## Create bloq

<mark style="color:green;">`POST`</mark> `https://api.bloqifi.com/v0/bloq`

Creates a new bloq

#### Headers

| Name                                            | Type   | Description                               |
| ----------------------------------------------- | ------ | ----------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | String | Bearer ${accessToken}                     |
| Content-Length                                  | String | Content length                            |
| blockchain<mark style="color:red;">\*</mark>    | String | `Bloqcoin`, `Dogecoin`, `Bitcoin`, `IPFS` |
| Cache-Control                                   | String | no-cache                                  |
| Content-Type<mark style="color:red;">\*</mark>  | String | application/json                          |
| folder                                          | String | ${folderId}                               |

#### Request Body

| Name                                                    | Type   | Description             |
| ------------------------------------------------------- | ------ | ----------------------- |
| multipart/form-data\*<mark style="color:red;">\*</mark> | String | Any appropriate content |

{% tabs %}
{% tab title="201: Created " %}

```javascript
{
    message: 'Original file name'
}
```

{% endtab %}

{% tab title="422: Unprocessable Entity " %}

```javascript
{
    message: 'Unprocessable Entity'
}
```

{% endtab %}
{% endtabs %}

The Chunked Upload API provides a way to reliably upload large files to Bloqifi by chunking them into a sequence of parts that can be uploaded individually. By using this API the application uploads a file in part, allowing it to recover from a failed request more reliably.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
export const deployToBloqifi = async ({name, description, blockchain, content, token}) => {

const formHash = {
  'dzuuid': 'Random-' + Date.now(),
  'dzchunkindex': '0',
  'dztotalfilesize': Buffer.byteLength(content),
  'dzchunksize': 1000000,
  'dztotalchunkcount': '1',
  'dzchunkbyteoffset': '0'
}

const boundary = formHash.dzuuid;
let body = '';

for (let key in formHash) {
  body += `-----------------------------${boundary}\r\n`;
  body += `Content-Disposition: form-data; name="${key}"\r\n\r\n`;
  body += `${formHash[key]}\r\n`;
}

body += `-----------------------------${boundary}\r\n`;
body += `Content-Disposition: form-data; name="file"; filename="${name}"\r\n`;
body += `Content-Type: application/octet-stream\r\n\r\n${content}\r\n`;
body += `-----------------------------${boundary}--`;

const rawResponse = await fetch('https://api.bloqifi.com/v0/bloq', {
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data; boundary=---------------------------' + boundary,    
    'Authorization': 'Bearer ' + token,
    'blockchain': blockchain,
    'Cache-Control': 'no-cache',
    'Content-Length': Buffer.byteLength(content)
  },
  body: body
});
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Good to know:** By using this API the application uploads a file in part, allowing it to recover from a failed request more reliably. It means an application only needs to retry the upload of a single part instead of the entire file.

An additional benefit of chunked uploads is that parts can be uploaded in parallel, allowing for a potential performance improvement.
{% endhint %}

## Create a new folder

You'll need a folder ID to merge chunks from a new bloq.

## Create new folder

<mark style="color:purple;">`PATCH`</mark> `https://api.bloqifi.com/v0/bloq`

Creates a new folder, or receive current folder id

#### Headers

| Name                                            | Type   | Description           |
| ----------------------------------------------- | ------ | --------------------- |
| Content-Type                                    | String | application/json      |
| Authorization<mark style="color:red;">\*</mark> | String | Bearer ${accessToken} |

#### Request Body

| Name                                         | Type   | Description                               |
| -------------------------------------------- | ------ | ----------------------------------------- |
| name<mark style="color:red;">\*</mark>       | String | Name of folder                            |
| blockchain<mark style="color:red;">\*</mark> | String | `Bloqcoin`, `Dogecoin`, `Bitcoin`, `IPFS` |

{% tabs %}
{% tab title="201: Created " %}

```javascript
{
    id: new ObjectId
}
```

{% endtab %}

{% tab title="400: Bad Request " %}

```javascript
{
    message: 'err'
}
```

{% endtab %}

{% tab title="200: OK " %}

```javascript
{
    id: ObjectId
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const rawResponse = await fetch('https://api.bloqifi.com/v0/bloq', {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    name: 'New Folder',
    blockchain: 'Bloqcoin'
  })
});

const json = await rawResponse.json();

// 201 Created
if (rawResponse.status === 201) {

  // grep folder id
  json.id
}

```

{% endtab %}
{% endtabs %}

## Merge chunks for created bloq

The merge and combine of multiple chunks in the created `bloq` could take a while for larger files, however not longer then any predefined timeouts.

## Merge chunks

<mark style="color:orange;">`PUT`</mark> `https://api.bloqifi.com/v0/bloq`

#### Headers

| Name                                            | Type   | Description           |
| ----------------------------------------------- | ------ | --------------------- |
| Authorization<mark style="color:red;">\*</mark> | String | Bearer ${accessToken} |
| Content-Type<mark style="color:red;">\*</mark>  | String | application/json      |

#### Request Body

| Name                                                | Type   | Description                               |
| --------------------------------------------------- | ------ | ----------------------------------------- |
| originalname<mark style="color:red;">\*</mark>      | String | Name of file                              |
| folder<mark style="color:red;">\*</mark>            | String | ${folderId}                               |
| dzuuid<mark style="color:red;">\*</mark>            | String | ${dzuuid}                                 |
| dztotalchunkcount<mark style="color:red;">\*</mark> | String | ${dztotalchunkcount}                      |
| dztotalfilesize<mark style="color:red;">\*</mark>   | String | ${dztotalfilesize}                        |
| blockchain<mark style="color:red;">\*</mark>        | String | `Bloqcoin`, `Dogecoin`, `Bitcoin`, `IPFS` |

{% tabs %}
{% tab title="201: Created " %}

```javascript
{
    message: 'Merged successfully..',
    id: new ObjectId,
    hexSize: 100
}
```

{% endtab %}

{% tab title="422: Unprocessable Entity " %}

<pre class="language-javascript"><code class="lang-javascript">{
<strong>    message: 'Unprocessable Entity'
</strong>}
</code></pre>

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const rawResponse = await fetch('https://api.bloqifi.com/v0/bloq', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    dzuuid: ${dzuuid},
    originalname: `Name of file`,
    dztotalfilesize: ${dztotalfilesize},
    dztotalchunkcount: ${dztotalchunkcount},
    blockchain: 'Bloqcoin',
    folder: ${ObjectId}
  })
});

const json = await rawResponse.json();

// 201 Created
if (rawResponse.status === 201) {

  // merge complete
}
```

{% endtab %}
{% endtabs %}

In some cases, creating the parts might take a long time and the API will return a `202 Accepted` status code instead.

{% hint style="info" %}
**Good to know:** If any of the blockchains is used (Bloqcoin, Dogecoin, Bitcoin) payment is required and you'll need the `json.id` to complete the next step.
{% endhint %}


---

# 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.bloqifi.com/reference/api-reference/bloq.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.
