A Golang wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Oauth to provide a secure connection to WooCommerce.
To install this WooCommerce REST API Golang Wrapper, use go get
:
go get github.com/Buccaneer69/wc-api-go
To update WooCommerce REST API Golang Wrapper to the latest version, use
go get -u github.com/Buccaneer69/wc-api-go
Generate API credentials (Consumer Key & Consumer Secret) following this instructions http://docs.woocommerce.com/document/woocommerce-rest-api/.
Check out the WooCommerce API endpoints and data that can be manipulated in https://woocommerce.github.io/woocommerce-rest-api-docs/.
Setup for the new WP REST API integration:
import "github.com/Buccaneer69/wc-api-go/client"
func main() {
factory := client.Factory{}
c := factory.NewClient(options.Basic{
URL: "http://example.com",
Key: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
Secret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
Options: options.Advanced{
WPAPI: true,
WPAPIPrefix: "/wp-json/",
Version: "wc/v3",
},
})
// Further using of client ...
}
Option | Type | Required | Description |
---|---|---|---|
URL |
string |
yes | Your Store URL, example: http://woo.dev/ |
Key |
string |
yes | Your API consumer key |
Secret |
string |
yes | Your API consumer secret |
Options |
struct |
no | Extra arguments (see client options table) |
Option | Type | Required | Description |
---|---|---|---|
WPAPI |
bool |
no | Allow make requests to the new WP REST API integration (WooCommerce 2.6 or later) |
WPAPIPrefix |
string |
no | Custom WP REST API URL prefix, used to support custom prefixes created with the rest_url_prefix filter |
Version |
string |
no | API version, default is v3 |
Timeout |
int |
no | Request timeout, default is 15 |
FollowRedirects |
bool |
no | Allow the API call to follow redirects |
VerifySsl |
bool |
no | Verify SSL when connect, use this option as false when need to test with self-signed certificates, default is true |
QueryStringAuth |
bool |
no | Force Basic Authentication as query string when true and using under HTTPS, default is false |
OAuthTimestamp |
string |
no | Custom oAuth timestamp, default is time() |
UserAgent |
string |
no | Custom user-agent, default is WooCommerce API Client-PHP |
Params | Type | Description |
---|---|---|
endpoint |
string |
WooCommerce API endpoint, example: customers or order/12 |
body |
interface |
Only for POST and PUT, data that will be converted to JSON |
parameters |
url.Values |
Only for GET and DELETE, request query string |
c.Get(endpoint, parameters)
c.Post(endpoint, nil, body)
c.Put(endpoint, nil, body)
c.Delete(endpoint, parameters)
c.Options(endpoint)
All methods will return *http.Response
on success and returning an error
on failure.
package main
import (
"fmt"
"github.com/Buccaneer69/wc-api-go/client"
"github.com/Buccaneer69/wc-api-go/options"
"io/ioutil"
"log"
"net/http"
)
func main() {
factory := client.Factory{}
c := factory.NewClient(options.Basic{
URL: "http://woo.dev/",
Key: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
Secret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
Options: options.Advanced{
WPAPI: true,
WPAPIPrefix: "/wp-json/",
Version: "wc/v3",
},
})
if r, err := c.Get("products", nil); err != nil {
log.Fatal(err)
} else if r.StatusCode != http.StatusOK {
log.Fatal("Unexpected StatusCode:", r)
} else {
defer r.Body.Close()
if bodyBytes, err := ioutil.ReadAll(r.Body); err != nil {
log.Fatal(err)
} else {
fmt.Println(string(bodyBytes))
}
}
}
- 2019-11-03 - 1.0.2 - Fix POST request
- 2019-04-14 - 1.0.1 - Fix sample integration setup in README.md
- 2019-01-12 - 1.0.0 - First Release