POS Integrations
HTTP API
Code examples

Code examples

Python

import io, requests, csv
 
# Define your upload key and your data dictionary
upload_key = '<<your upload key>>'
data = [{'barcode': '000001', 'price': '1.00', 'quantity': 1}]
 
# Turn data dictionary into a CSV string
fh = io.StringIO('')
writer = csv.DictWriter(fh, fieldnames=list(data[0].keys()))
writer.writeheader()
for row in data:
    writer.writerow(row)
fh.seek(0)
 
# Request upload URL
url = requests.get(f"https://stock.near.live/upload/{upload_key}?type=stock").text
 
# PUT request to upload CSV string
requests.put(url, data=fh.read(), headers={'content-type': 'text/csv'}).text

JavaScript (ES6, fetch)

This example expects a File object (opens in a new tab) to be set as variable file, and a string named uploadKeyto contain the shop's upload key.

// Get upload URL for our shop
const res = await fetch(`https://stock.near.live/upload/${uploadKey}`)
const url = await res.text();
console.log(`Upload URL: ${url}`);
 
// Do a PUT request with our File object
await fetch(url, { method: "PUT", body: file });
console.log('Uploaded file successfully');

PHP (curl)

This code snippet expects a stock.csv file to be saved to disk. If you need an example on how to create this file from an array of data, have a look at our FTP PHP example.

<?php
 
// Define upload key and file path
$upload_key = '<<your upload key>>';
$csv_path = 'stock.csv';
 
// Get upload URL
$url = file_get_contents('https://stock.near.live/upload/' . $upload_key);
 
// Upload file to URL using CURL and PUT request
$fh_res = fopen($csv_path, 'r');
$ch = curl_init();
curl_setopt_array($ch, [
	CURLOPT_URL => $url,
	CURLOPT_PUT => 1,
	CURLOPT_INFILE => $fh_res,
	CURLOPT_INFILESIZE => filesize($csv_path),
	CURLOPT_HTTPHEADER => [
		'Content-Type: text/csv'
	]
]);
curl_exec($ch);
fclose($fh_res);
 
// Output status code (should be 200)
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);

C# (Unirest)

This example uses the library Unirest (opens in a new tab) to do the HTTP calls, but something similar should work with any other HTTP library.

// Define upload URL and CSV data
string uploadKey = "<<your upload key>>";
string data = "barcode,...";
 
// Get upload URL
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var url = Unirest.get($"https://stock.near.live/upload/{uploadKey}")
    .asString().Body;
 
// Upload CSV data
var response = Unirest.put(url)
    .header("Accept", "application/json")
    .header("Content-Type", "text/csv")
    .body(data)
    .asString();
 
// Check result (response.Code should be 200)
Console.WriteLine($"Response code: {response.Code} {response.Body}");

PowerShell

A great low-tech way to get file uploads working in a PowerShell script that you execute on a timer (opens in a new tab) .

# Update these variables
$csvPath = "C:\local\path\stock.csv" # Point this to your CSV file
$uploadKey = "xxxxxxxxx" # Set your Upload key / FTP password here
 
# Everything below here can stay the same
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
 
# Get upload URL
$uploadUri = (Invoke-WebRequest -Uri "https://stock.near.live/upload/${uploadKey}?type=stock").Content
 
# Perform PUT
$wc = New-Object System.Net.WebClient
$wc.Headers.add('content-type','text/csv')
$wc.UploadFile($uploadUri, "PUT", $csvPath)
 
Write-Output "File uploaded to NearSt!"