# Import usage from CSV

Upload usage data through the 2Checkout API from a CSV (comma-separated values) file for active and trial subscriptions. Usage data uploaded via JSON-RPC APIv6 will be used to charge subscribers for the metered resources they consume. Metered billing allows for usage fees in arrears to be added to recurring subscription costs.

2Checkout will capture the parameters sent via JSON-RPC APIv6 and import the usage data into the system. Once the upload process is complete, you'll be able to access the uploaded file in the [Usage](https://secure.avangate.com/cpanel/payperusage_management.php) area of the Merchant Control Panel.

{% hint style="info" %}
Set the DATE parameter to GMT+2 time zone, when making the API call to the Usage Upload endpoint.
{% endhint %}

## Method and URL

**POST**: <https://api.2checkout.com/subscription/payperusage/receiver/index.php&#x20>;

## Request parameters

<table><thead><tr><th width="162.60009765625">Parameter</th><th width="444.5333251953125">Type / Description</th><th>Used in HASH validation*</th></tr></thead><tbody><tr><td><code>merchant</code></td><td><p><strong>Required (string)</strong></p><p>Your unique 2Checkout merchant code. <a href="https://secure.2checkout.com/cpanel/account_settings.php">View merchant code</a>.</p></td><td>YES</td></tr><tr><td><code>secret_key</code></td><td><p><strong>Required (string)</strong></p><p>The secret key associated to your account. <a href="https://secure.2checkout.com/cpanel/account_settings.php">View secret key</a>.</p></td><td>YES</td></tr><tr><td><code>file_MD5</code></td><td><p><strong>Required (string)</strong></p><p>The md5 hash of the CSV file used to upload usage.</p></td><td>YES</td></tr><tr><td><code>hash</code></td><td><p><strong>Required (string)</strong></p><p>The MD5 hmac key for the request.</p></td><td>N/A</td></tr><tr><td><code>UsageStart</code></td><td><p><strong>Required (datetime)</strong></p><p>The datetime when the usage started; can be the same as <code>UsageEnd</code>. For existing records, this will be updated to time 00:00:00.</p></td><td>N/A</td></tr><tr><td><code>UsageEnd</code></td><td><p><strong>Required (datetime)</strong></p><p>The datetime when the usage ended and was recorded.  For existing records this will be updated to time 23:59:59, this will be enforced in the CSV upload as well.</p></td><td>N/A</td></tr><tr><td><code>OptionCode</code></td><td><p><strong>Required (string)</strong></p><p>Unique codes you provided as identifiers of specific Pricing Options in the Usage Scale Pricing Options Group scheme. Example: metered pricing.</p></td><td>N/A</td></tr><tr><td><code>Description</code></td><td><p><strong>Optional (string)</strong></p><p>It can be used to store a short merchant comment of the usage being uploaded. This can be anything, from the source of usage (mobile, web, etc.), to why changes occurred, etc. Example: Subscription usage for September.</p></td><td>N/A</td></tr><tr><td><code>SubscriptionRef</code></td><td><p><strong>Required (string)</strong></p><p>Unique code that represents a subscription. Example: 83FE4FEF2.</p></td><td>N/A</td></tr></tbody></table>

### Request sample

```php
<?php
//hmac
$date             = date("Y-m-d");
$fileReceived_md5 = md5_file("[Path to the CSV file used for usage upload]");
$merchantCode     = "";
/* 2Checkout Code */
$merchantKey      = "";
/* Merchant Secret Key */
$hashParams       = "MERCHANT={$merchantCode}&FILE={$fileReceived_md5}&DATE={$date}";
$hmac             = hash_hmac('md5', $hashParams, $merchantKey);
?>
```

```html
<html>
<body>
<form method="post" action="https://api.2checkout.com/subscription/payperusage/receiver/index.php" enctype="multipart/form-data">
<input type="text" name="MERCHANT" value="<?php
echo $merchantCode;
?>" /><br />
<input type="text" name="HASH" value="<?php
echo $hmac;
?>" /><br />
<input type="file" name="OPTIONS" /><br />
<input type="hidden" name="FILE_MD5" value="<?php
echo $fileReceived_md5;
?>" />
<input type="hidden" name="DATE" value="<?php
echo $date;
?>" />
<input type="submit" /><br />
</form>
</body>
</html>
```

## Error handling

| Message code                        | Message description                                                                   | Resolution                                                                                                                                                     |
| ----------------------------------- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1                                   | The upload operation completed successfully.                                          | N/A                                                                                                                                                            |
| Invalid account!                    | The merchant code provided does not belong to a vendor in the 2Checkout system.       | Please check your merchant code and make sure you're using the correct version. [View merchant code](https://secure.avangate.com/cpanel/account_settings.php). |
| Bad signature on the received file! | The upload CSV file's md5 signature is different than the one sent as a parameter.    | Please make sure that you're using the correct md5 signature for the CSV file, or that you're uploading the CSV file for which the signature was generated.    |
| Invalid signature                   | The HMAC calculated by the 2Checkout system is different from the one you're sending. | Please make sure that all parameters use correct values.                                                                                                       |


---

# 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.2checkout.com/json-rpc-api-reference/json-rpc-api-6.0/api-requests/usage-management/import-usage-from-csv.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.
