Skip to content
A Nim wrapper for the WooCommerce REST API
Nim
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
tests
woocommerce
LICENSE
README.md
woocommerce.nimble

README.md

WooCommerce API - Nim Client

A Nim wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API using this library.

Installation

git clone https://github.com/mrhdias/woocommerce-api-nim
cd woocommerce-api-nim
nimble install

Getting started

Generate API credentials (Consumer Key & Consumer Secret) following this instructions http://woocommerce.github.io/woocommerce-rest-api-docs/#rest-api-keys.

Check out the WooCommerce API endpoints and data that can be manipulated in http://woocommerce.github.io/woocommerce-rest-api-docs/.

Setup

  • Synchronous API
import woocommerce/API

let wcapi = API(
  url="http://example.com", # Your store URL
  consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # Your consumer key
  consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Your consumer secret
)
  • Asynchronous API
import woocommerce/API

let wcapi = AsyncAPI(
  url="http://example.com",
  consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)

Methods

Params Type Description
endpoint string WooCommerce API endpoint, example: products or order/12
data string JSON data stringified
params seq[(string, string)] Accepts params to be passed as a query string
  • GET
# Retrieve
wcapi.get(endpoint: string; params: seq[(string, string)) # params is optional
  • POST
# Create
wcapi.post(endpoint: string, data: string)
  • PUT
# Update
wcapi.put(endpoint: string, data: string)
  • DELETE
# Delete
wcapi.delete(endpoint: string; params: seq[(string, string)) # params is optional
  • OPTIONS
# JSON Schema
wcapi.options(endpoint: string)

Response

All methods will return Response / AsyncResponse object.

Example of returned data for asynchronous API:

import woocommerce/API
import asyncdispatch
import json

proc main() {.async.} =
  let wcapi = AsyncAPI(
    url="http://example.com",
    consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  )

  response = await wcapi.get("products?status=publish&per_page=10&page=1")
  echo response.status
  if response.status == "200 OK":
    let products = parseJson(response.body)
    for product in products:
      echo "SKU:", product["sku"], " NAME:", product["name"]

  wcapi.close()

waitFor main()

Request with params example

import woocommerce/API
import asyncdispatch
import json

proc main() {.async.} =
  let wcapi = AsyncAPI(
    url="http://example.com",
    consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  )

  response = await wcapi.get(
    "products",
    params = @{"status": "publish", "per_page": $(10), "page": $(2)}
  )
  echo response.status
  if response.status == "200 OK":
    let products = parseJson(response.body)
    for product in products:
      echo "SKU:", product["sku"], " NAME:", product["name"]

  wcapi.close()

waitFor main()
You can’t perform that action at this time.