Code Examples

These examples target the current upload API (/api/v1/data-sources).

cURL / Bash

ARCHIVE="sales_upload.zip"
TOKEN="YOUR_TOKEN"
CHECKSUM=$(md5sum "$ARCHIVE" | awk '{print $1}')

CREATE_RESPONSE=$(curl -sS -X POST "https://api.ecue.ai/api/v1/data-sources" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "Content-Type: application/zip" \
  -H "X-Checksum: ${CHECKSUM}" \
  --data-binary "@${ARCHIVE}")

echo "$CREATE_RESPONSE"
SOURCE_ID=$(echo "$CREATE_RESPONSE" | jq -r '.source_id')

curl -sS -X GET "https://api.ecue.ai/api/v1/data-sources/${SOURCE_ID}" \
  -H "Authorization: Bearer ${TOKEN}"

Python

import hashlib
import requests

API_BASE = "https://api.ecue.ai"
TOKEN = "YOUR_TOKEN"
ARCHIVE_PATH = "sales_upload.zip"


def md5sum(path: str) -> str:
    h = hashlib.md5()  # nosec - API contract requires MD5 transport checksum
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(8192), b""):
            h.update(chunk)
    return h.hexdigest()


checksum = md5sum(ARCHIVE_PATH)
headers = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/zip",
    "X-Checksum": checksum,
}

with open(ARCHIVE_PATH, "rb") as archive_file:
    create_resp = requests.post(
        f"{API_BASE}/api/v1/data-sources",
        headers=headers,
        data=archive_file,
        timeout=120,
    )

create_resp.raise_for_status()
created = create_resp.json()
print(created)

status_resp = requests.get(
    f"{API_BASE}/api/v1/data-sources/{created['source_id']}",
    headers={"Authorization": f"Bearer {TOKEN}"},
    timeout=30,
)
status_resp.raise_for_status()
print(status_resp.json())

Go

package main

import (
    "crypto/md5" //nolint:gosec // API requires MD5 checksum header
    "encoding/hex"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"
)

type CreateResponse struct {
    SourceID  string `json:"source_id"`
    TableName string `json:"table_name"`
    Status    string `json:"status"`
}

func md5sum(path string) (string, error) {
    f, err := os.Open(path)
    if err != nil {
        return "", err
    }
    defer f.Close()

    h := md5.New() //nolint:gosec
    if _, err := io.Copy(h, f); err != nil {
        return "", err
    }

    return hex.EncodeToString(h.Sum(nil)), nil
}

func main() {
    apiBase := "https://api.ecue.ai"
    token := "YOUR_TOKEN"
    archivePath := "sales_upload.zip"

    checksum, err := md5sum(archivePath)
    if err != nil {
        panic(err)
    }

    archiveFile, err := os.Open(archivePath)
    if err != nil {
        panic(err)
    }
    defer archiveFile.Close()

    req, err := http.NewRequest("POST", apiBase+"/api/v1/data-sources", archiveFile)
    if err != nil {
        panic(err)
    }

    req.Header.Set("Authorization", "Bearer "+token)
    req.Header.Set("Content-Type", "application/zip")
    req.Header.Set("X-Checksum", checksum)

    createResp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    defer createResp.Body.Close()

    if createResp.StatusCode != http.StatusCreated {
        body, _ := io.ReadAll(createResp.Body)
        panic(fmt.Sprintf("create failed: %s", string(body)))
    }

    var created CreateResponse
    if err := json.NewDecoder(createResp.Body).Decode(&created); err != nil {
        panic(err)
    }

    fmt.Printf("created source %s\n", created.SourceID)
}