Skip to content

UploadFile#

Test Postman Apidog

For the UploadFile method, we recommend to use the mediaUrl host from console

The method is designed to upload a file to the cloud storage, which can be sent using the sendFileByUrl method. It also allows you to avoid errors in receiving files from external storages and speed up sending messages, for example, in a mailing script.

An alternative way to send a file by uploading from disk: sendFileByUpload.

It is recommended to use the method when sending messages of the same type:

  1. Using the UploadFile method, upload the required file to the Green-API storage, receive a link to the file in the response.
  2. Use the SendFileByUrl method to send using the received link.

Link lifetime is 15 days.

The maximum file size that can be sent is 100 MB.

The type of file you send and how you send it is determined by the file extension.

List of file extensions, that could be successfully sent and received via the API

Request#

To download a file, you need to make a request to the address:

POST
{{mediaUrl}}/waInstance{{idInstance}}/uploadFile/{{apiTokenInstance}}

See the Before you start section to obtain the mediaUrl, idInstance and apiTokenInstance query parameters.

Request headers#

Parameter Mandatory Description
Content-Type Yes Тhe file type in accordance with the list of MIME types . (If the file type is not in the list, then GA-Filename is passed instead of this string)
GA-Filename No The file name with the extension, if the file is not in the list of MIME types. For example, files as .go or .py. (If the file type matches the list, the string is not transmitted, only Content-Type is used)

If the system cannot determine the file type, it will be sent with the default type (binary file): .bin

Request parameters#

Parameter Type Mandatory Description
file binary Yes Upload file

Request body example#

You must specify the file itself as the body of the request.

Response#

Response fields#

Field Type Description
urlFile string Link to uploaded file
How to obtain information about the file upload deadline

To obtain the information about the file upload deadline, you need to retrieve information from the link header

To do this:

  1. Obtain the link to the file
  2. Execute a HEAD request with the obtained link

    head-request

  3. From the result, retrieve the value of the X-Amz-Expiration field

  4. The obtained value indicates until when the link will be available.

As an example the X-Amz-Expiration field, contains the data expiry-date="Fri, 31 May 2024 00:00:00 GMT". Which denotes that the link is available until 05/31/2024 00:00:00 by GMT time zone.

head-result

Response body example#

{
     "urlFile": "https://sw-media-out.storage.yandexcloud.net/1101123456/c1aabd48-c1c2-49b1-8f2d-f575a41777be.jpg"
}

Errors uploadFile#

For a list of Common errors for all methods, see the Common errors section

HTTP code Error ID Description
400 file should not be empty The user submitted an empty file. The uploaded file must not be empty.
413 request entity too large Exceeded file size limit (~99.9mb).
Occurs when sending files in 1C processing (version 8.3.22.1923). Possible solution: Change 1C version.
500 Internal Server Error Incorrect method used for request. Change the method to POST and try again

Code examples#

Code example using methods uploadFile + sendFileByUrl#

<?php
// Need to use SDK https://github.com/green-api/whatsapp-api-client-php
require './vendor/autoload.php';

use GreenApi\RestApi\GreenApiClient;

define( "ID_INSTANCE", "1101123456" );
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE, 'http://127.0.0.1:8080' );

$result = $greenApi->sending->uploadFile(
     'C:\Games\PicFromDisk.png'
);

print_r( $result->data );

$result = $greenApi->sending->sendFileByUrl(
     '11001234567@c.us',
   $result->data->urlFile,
   'googlelogo_color_272x92dp.png',
  'Google logo'
);

print_r( $result->data );

Code examples uploadFile#

import requests
url = "{{mediaUrl}}/waInstance{{idInstance}}/uploadFile/{{apiTokenInstance}}"

files = [
('file', ('window.jpg', open('C:/window.jpg','rb'),'image/jpeg'))
]
headers = {
'Content-Type': 'image/jpeg'
}

response = requests.post(url, files=files, headers=headers)

print(response.text.encode('utf8'))
curl --location '{{mediaUrl}}/{{idInstance}}/uploadFile/{{apiTokenInstance}}' \
--header 'Content-Type: image/jpeg' \
--data-binary "@/Users/user/Desktop/fileExample.jpeg"
var file = new File("/Users/user/Desktop/fileExample.jpeg");
var restTemplate = new RestTemplate();
var requestUrl = new StringBuilder();
requestUrl
    .append({{mediaUrl}})
    .append("/waInstance").append({{idInstance}})
    .append("/uploadFile/")
    .append({{apiTokenInstance}});

var byteArrayResource = new ByteArrayResource(Files.readAllBytes(file.toPath()));

var headers = new HttpHeaders();
headers.setContentType(MediaTypeFactory.getMediaType(file.getName())
    .orElse(MediaType.APPLICATION_OCTET_STREAM));

var requestEntity = new HttpEntity<>(byteArrayResource, headers);

var response = restTemplate.exchange(requestUrl.toString(), HttpMethod.POST, requestEntity, String.class);
System.out.println(response);
var file = new File("/Users/user/Desktop/fileExample.jpeg");
var requestUrl = new StringBuilder();
requestUrl
    .append({{mediaUrl}})
    .append("/waInstance").append({{idInstance}})
    .append("/uploadFile/")
    .append({{apiTokenInstance}});

var response = Unirest.post(requestUrl.toString())
    .header("Content-Type", Files.probeContentType(file.toPath()))
    .body(Files.readAllBytes(file.toPath()))
    .asString();

System.out.println(response);
<?php
// Need to use SDK https://github.com/green-api/whatsapp-api-client-php
require './vendor/autoload.php';

use GreenApi\RestApi\GreenApiClient;

define( "ID_INSTANCE", "1101123456" );
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );

$result = $greenApi->sending->uploadFile(
    'C:\Games\PicFromDisk.png'
);

print_r( $result->data );