File Uploads
Files can be uploaded as a way to import coupon codes. Files need to be uploaded to S3 and then a coupon batch needs to be created using the Builder API to actually import the codes.
Files should be uploaded directly to AWS S3. Details are provided in the portal. The S3 bucket name and region are provided in the "Organisation Details" section and the S3 "Bucket Folder Name" (prefix) is provided in each project section.
In order to access the S3 bucket to upload files you need to obtain temporary credentials. This is done using the Tokens API endpoint in the Builder API. The temporary token obtained from the tokens API only has access to the prefix (folder) for the project.
Virus Scanning
When a file is uploaded to S3 the file is scanned for viruses using the Cloudmersive virus scanning API. If the file is clean it is copied to a different S3 bucket that the coupon code import process uses. When a coupon batch using a file is started the process first checks if the file has been marked as clean or not. If Cloudmersive determined the file contained viruses the batch will be marked as errored.
Example Code
Obtain Temporary token for S3
Below is the request to get the credentials for S3.
curl -X 'POST' \
'https://discounts-management.dovetech.com/tokens' \
-H 'accept: application/json' \
-H 'x-api-key: [Builder API Key]' \
-d ''
Example response is below:
{
"accessKeyId": "[accessKeyId]",
"sessionToken": "[sessionToken]",
"secretAccessKey": "[secretAccessKey]",
"expirationUtc": "2023-03-17T14:19:42Z"
}
Calling S3 APIs
Examples for Node JS are provided below. These use the @aws-sdk/client-s3
npm module.
import { S3Client, ListObjectsCommand } from '@aws-sdk/client-s3';
const handler = async () => {
const client = new S3Client({
region: 'eu-west-2',
credentials: {
accessKeyId: '[accessKeyId]',
secretAccessKey: '[secretAccessKey]',
sessionToken: '[sessionToken]',
},
});
const params = {
Bucket: 'dev-dovetech-campaigns-customer-files',
Prefix: '983cf511397942bfaadb7a61cb3a2fa9',
};
const result = await client.send(new ListObjectsCommand(params));
console.log(result);
};
handler();
Coupon Code File Upload Example
Below is an example of uploading a file (this example just creates an empty file).
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const handler = async () => {
const client = new S3Client({
region: "eu-west-2",
credentials: {
accessKeyId: "[accessKeyId]",
secretAccessKey: "[secretAccessKey]",
sessionToken: "[sessionToken]",
},
});
const putObjectCommand = new PutObjectCommand({
Bucket: "dev-dovetech-campaigns-customer-files",
Key: "983cf511397942bfaadb7a61cb3a2fa9/coupons.csv",
});
const result = await client.send(putObjectCommand);
console.log(result);
};
handler();
Importing the File
To import the coupon codes in the file you create a coupon code batch. An example request is below:
curl -X 'POST' \
'https://discounts-management.dovetech.com/couponcodebatches' \
-H 'accept: */*' \
-H 'x-api-key: [Builder API Key]' \
-H 'Content-Type: application/json' \
-d '{
"type": "Upload",
"groupId": "644151a7-74f1-44c7-8352-427aa28d38b3",
"fileName": "983cf511397942bfaadb7a61cb3a2fa9/coupons.csv"
}'