Categorygithub.com/marinx/go-shopify
modulepackage
2.3.0+incompatible
Repository: https://github.com/marinx/go-shopify.git
Documentation: pkg.go.dev

# README

go-shopify

The new home of Conversio's Shopify Go library.

Note: The library does not have implementations of all Shopify resources, but it is being used in production and should be stable for usage. PRs for new resources and endpoints are welcome, or you can simply implement some yourself as-you-go. See the section "Using your own models" for more info.

Build Status codecov Join the chat at https://gitter.im/bold-commerce/go-shopify

Install

$ go get github.com/bold-commerce/go-shopify

Use

import "github.com/bold-commerce/go-shopify"

This gives you access to the goshopify package.

Oauth

If you don't have an access token yet, you can obtain one with the oauth flow. Something like this will work:

// Create an app somewhere.
app := goshopify.App{
    ApiKey: "abcd",
    ApiSecret: "efgh",
    RedirectUrl: "https://example.com/shopify/callback",
    Scope: "read_products,read_orders",
}

// Create an oauth-authorize url for the app and redirect to it.
// In some request handler, you probably want something like this:
func MyHandler(w http.ResponseWriter, r *http.Request) {
    shopName := r.URL.Query().Get("shop")
    authUrl := app.AuthorizeURL(shopName)
    http.Redirect(w, r, authUrl, http.StatusFound)
}

// Fetch a permanent access token in the callback
func MyCallbackHandler(w http.ResponseWriter, r *http.Request) {
    // Check that the callback signature is valid
    if ok, _ := app.VerifyAuthorizationURL(r.URL); !ok {
        http.Error(w, "Invalid Signature", http.StatusUnauthorized)
        return
    }

    query := r.URL.Query()
    shopName := query.Get("shop")
    code := query.Get("code")
    token, err := app.GetAccessToken(shopName, code)

    // Do something with the token, like store it in a DB.
}

Api calls with a token

With a permanent access token, you can make API calls like this:

// Create an app somewhere.
app := goshopify.App{
    ApiKey: "abcd",
    ApiSecret: "efgh",
    RedirectUrl: "https://example.com/shopify/callback",
    Scope: "read_products",
}

// Create a new API client
client := goshopify.NewClient(app, "shopname", "token")

// Fetch the number of products.
numProducts, err := client.Product.Count(nil)

Private App Auth

Private Shopify apps use basic authentication and do not require going through the OAuth flow. Here is an example:

// Create an app somewhere.
app := goshopify.App{
	ApiKey: "apikey",
	Password: "apipassword",
}

// Create a new API client (notice the token parameter is the empty string)
client := goshopify.NewClient(app, "shopname", "")

// Fetch the number of products.
numProducts, err := client.Product.Count(nil)

Query options

Most API functions take an options interface{} as parameter. You can use one from the library or create your own. For example, to fetch the number of products created after January 1, 2016, you can do:

// Create standard CountOptions
date := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
options := goshopify.CountOptions{createdAtMin: date}

// Use the options when calling the API.
numProducts, err := client.Product.Count(options)

The options are parsed with Google's go-querystring library so you can use custom options like this:

// Create custom options for the orders.
// Notice the `url:"status"` tag
options := struct {
    Status string `url:"status"`
}{"any"}

// Fetch the order count for orders with status="any"
orderCount, err := client.Order.Count(options)

Using your own models

Not all endpoints are implemented right now. In those case, feel free to implement them and make a PR, or you can create your own struct for the data and use NewRequest with the API client. This is how the existing endpoints are implemented.

For example, let's say you want to fetch webhooks. There's a helper function Get specifically for fetching stuff so this will work:

// Declare a model for the webhook
type Webhook struct {
    ID int         `json:"id"`
    Address string `json:"address"`
}

// Declare a model for the resource root.
type WebhooksResource struct {
    Webhooks []Webhook `json:"webhooks"`
}

func FetchWebhooks() ([]Webhook, error) {
    path := "admin/webhooks.json"
    resource := new(WebhooksResource)
    client := goshopify.NewClient(app, "shopname", "token")

    // resource gets modified when calling Get
    err := client.Get(path, resource, nil)

    return resource.Webhooks, err
}

Webhooks verification

In order to be sure that a webhook is sent from ShopifyApi you could easily verify it with the VerifyWebhookRequest method.

For example:

func ValidateWebhook(httpRequest *http.Request) (bool) {
    shopifyApp := goshopify.App{ApiSecret: "ratz"}
    return shopifyApp.VerifyWebhookRequest(httpRequest)
}

Develop and test

There's nothing special to note about the tests except that if you have Docker and Compose installed, you can test like this:

$ docker-compose build dev
$ docker-compose run --rm dev

Testing the package is the default command for the dev container. To create a coverage profile:

$ docker-compose run --rm dev bash -c 'go test -coverprofile=coverage.out ./... && go tool cover -html coverage.out -o coverage.html'

# Functions

No description provided by the author
Return the prefix for a fulfillment path.
Return the prefix for a metafield path.
Returns a new Shopify API client with an already authenticated shopname and token.
Return the Shop's base url.
Return the full shop name, including .myshopify.com.
Return the short shop name, excluding .myshopify.com.
WithVersion optionally sets the api-version if the passed string is valid.

# Constants

No description provided by the author

# Structs

No description provided by the author
App represents basic app settings such as Api key, secret, scope, and redirect url.
No description provided by the author
ApplicationChargeResource represents the result from the admin/application_charges{/X{/activate.json}.json}.json endpoints.
No description provided by the author
ApplicationChargesResource represents the result from the admin/application_charges.json endpoint.
AppliedDiscount is the discount applied to the line item or the draft order object.
Asset represents a Shopify asset.
AssetResource is the result from the themes/x/assets.json?asset[key]= endpoint.
AssetServiceOp handles communication with the asset related methods of the Shopify API.
AssetsResource is the result from the themes/x/assets.json endpoint.
Blog represents a Shopify blog.
Represents the result from the blogs/X.json endpoint.
BlogServiceOp handles communication with the blog related methods of the Shopify API.
BlogsResource is the result from the blogs.json endpoint.
Client manages communication with the Shopify API.
No description provided by the author
Collect represents a Shopify collect.
Represents the result from the collects/X.json endpoint.
CollectServiceOp handles communication with the collect related methods of the Shopify API.
Represents the result from the collects.json endpoint.
General count options that can be used for most collection counts.
CustomCollection represents a Shopify custom collection.
CustomCollectionResource represents the result form the custom_collections/X.json endpoint.
CustomCollectionServiceOp handles communication with the custom collection related methods of the Shopify API.
CustomCollectionsResource represents the result from the custom_collections.json endpoint.
Customer represents a Shopify customer.
CustomerAddress represents a Shopify customer address.
CustomerAddressResoruce represents the result from the customers/X/addresses.json endpoint.
CustomerAddressResoruce represents the result from the addresses/X.json endpoint.
CustomerAddressServiceOp handles communication with the customer address related methods of the Shopify API.
Represents the result from the customers/X.json endpoint.
Represents the options available when searching for a customer.
CustomerServiceOp handles communication with the product related methods of the Shopify API.
Represents the result from the customers.json endpoint.
Represents the result from the customers/tags.json endpoint.
No description provided by the author
DiscountCodeResource represents the result from the discount_codes/X.json endpoint.
DiscountCodeServiceOp handles communication with the discount code related methods of the Shopify API.
DiscountCodesResource is the result from the discount_codes.json endpoint.
DraftOrder represents a shopify draft order.
DraftOrderCountOptions represents the possible options to the count draft orders endpoint.
DraftOrderInvoice is the struct used to create an invoice for a draft order.
No description provided by the author
DraftOrderListOptions represents the possible options that can be used to further query the list draft orders endpoint.
No description provided by the author
DraftOrderServiceOp handles communication with the draft order related methods of the Shopify API.
No description provided by the author
Fulfillment represents a Shopify fulfillment.
FulfillmentResource represents the result from the fulfillments/X.json endpoint.
FulfillmentServiceOp handles communication with the fulfillment related methods of the Shopify API.
FulfillmentsResource represents the result from the fullfilments.json endpoint.
Image represents a Shopify product's image.
ImageResource represents the result form the products/X/images/Y.json endpoint.
ImageServiceOp handles communication with the image related methods of the Shopify API.
ImagesResource represents the result from the products/X/images.json endpoint.
InventoryItem represents a Shopify inventory item.
InventoryItemResource is used for handling single item requests and responses.
InventoryItemServiceOp is the default implementation of the InventoryItemService interface.
InventoryItemsResource is used for handling multiple item responsees.
No description provided by the author
No description provided by the author
General list options that can be used for most collections of entities.
No description provided by the author
Represents the result from the locations/X.json endpoint.
LocationServiceOp handles communication with the location related methods of the Shopify API.
Represents the result from the locations.json endpoint.
Metafield represents a Shopify metafield.
MetafieldResource represents the result from the metafields/X.json endpoint.
MetafieldServiceOp handles communication with the metafield related methods of the Shopify API.
MetafieldsResource represents the result from the metafields.json endpoint.
No description provided by the author
Order represents a Shopify order.
A struct for all available order count options.
A struct for all available order list options.
Represents the result from the orders/X.json endpoint.
OrderServiceOp handles communication with the order related methods of the Shopify API.
Represents the result from the orders.json endpoint.
Page represents a Shopify page.
PageResource represents the result from the pages/X.json endpoint.
PageServiceOp handles communication with the page related methods of the Shopify API.
PagesResource represents the result from the pages.json endpoint.
No description provided by the author
PriceRuleDiscountCode represents a Shopify Discount Code.
Product represents a Shopify product.
The options provided by Shopify.
Represents the result from the products/X.json endpoint.
ProductServiceOp handles communication with the product related methods of the Shopify API.
Represents the result from the products.json endpoint.
An error specific to a rate-limiting response.
Receipt represents a Shopify receipt.
RecurringApplicationCharge represents a Shopify RecurringApplicationCharge.
RecurringApplicationChargeResource represents the result from the admin/recurring_application_charges{/X{/activate.json}.json}.json endpoints.
RecurringApplicationChargeServiceOp handles communication with the RecurringApplicationCharge related methods of the Shopify API.
RecurringApplicationChargesResource represents the result from the admin/recurring_application_charges.json endpoint.
Redirect represents a Shopify redirect.
RedirectResource represents the result from the redirects/X.json endpoint.
RedirectServiceOp handles communication with the redirect related methods of the Shopify API.
RedirectsResource represents the result from the redirects.json endpoint.
No description provided by the author
No description provided by the author
ResponseDecodingError occurs when the response body from Shopify could not be parsed.
A general response error that follows a similar layout to Shopify's response errors, i.e.
No description provided by the author
ScriptTag represents a Shopify ScriptTag.
The options provided by Shopify.
ScriptTagResource represents the result from the admin/script_tags/{#script_tag_id}.json endpoint.
ScriptTagServiceOp handles communication with the shop related methods of the Shopify API.
ScriptTagsResource represents the result from the admin/script_tags.json endpoint.
No description provided by the author
Shop represents a Shopify shop.
Represents the result from the admin/shop.json endpoint.
ShopServiceOp handles communication with the shop related methods of the Shopify API.
SmartCollection represents a Shopify smart collection.
SmartCollectionResource represents the result from the smart_collections/X.json endpoint.
SmartCollectionServiceOp handles communication with the smart collection related methods of the Shopify API.
SmartCollectionsResource represents the result from the smart_collections.json endpoint.
StorefrontAccessToken represents a Shopify storefront access token.
StorefrontAccessTokenResource represents the result from the admin/storefront_access_tokens.json endpoint.
StorefrontAccessTokenServiceOp handles communication with the storefront access token related methods of the Shopify API.
StorefrontAccessTokensResource is the root object for a storefront access tokens get request.
No description provided by the author
Theme represents a Shopify theme.
Options for theme list.
ThemeServiceOp handles communication with the theme related methods of the Shopify API.
ThemesResource is the result from the themes.json endpoint.
No description provided by the author
TransactionResource represents the result from the orders/X/transactions/Y.json endpoint.
TransactionServiceOp handles communication with the transaction related methods of the Shopify API.
TransactionsResource represents the result from the orders/X/transactions.json endpoint.
UsageCharge represents a Shopify UsageCharge.
UsageChargeResource represents the result from the /admin/recurring_application_charges/X/usage_charges/X.json endpoints.
UsageChargeServiceOp handles communication with the UsageCharge related methods of the Shopify API.
UsageChargesResource represents the result from the admin/recurring_application_charges/X/usage_charges.json endpoint.
Variant represents a Shopify variant.
VariantResource represents the result from the variants/X.json endpoint.
VariantServiceOp handles communication with the variant related methods of the Shopify API.
VariantsResource represents the result from the products/X/variants.json endpoint.
Webhook represents a Shopify webhook.
WebhookOptions can be used for filtering webhooks on a List request.
WebhookResource represents the result from the admin/webhooks.json endpoint.
WebhookServiceOp handles communication with the webhook-related methods of the Shopify API.
WebhooksResource is the root object for a webhook get request.

# Interfaces

ApplicationChargeService is an interface for interacting with the ApplicationCharge endpoints of the Shopify API.
AssetService is an interface for interfacing with the asset endpoints of the Shopify API.
BlogService is an interface for interfacing with the blogs endpoints of the Shopify API.
CollectService is an interface for interfacing with the collect endpoints of the Shopify API.
CustomCollectionService is an interface for interacting with the custom collection endpoints of the Shopify API.
CustomerAddressService is an interface for interfacing with the customer address endpoints of the Shopify API.
CustomerService is an interface for interfacing with the customers endpoints of the Shopify API.
DiscountCodeService is an interface for interfacing with the discount endpoints of the Shopify API.
DraftOrderService is an interface for interfacing with the draft orders endpoints of the Shopify API.
FulfillmentService is an interface for interfacing with the fulfillment endpoints of the Shopify API.
FulfillmentsService is an interface for other Shopify resources to interface with the fulfillment endpoints of the Shopify API.
ImageService is an interface for interacting with the image endpoints of the Shopify API.
InventoryItemService is an interface for interacting with the inventory items endpoints of the Shopify API See https://help.shopify.com/en/api/reference/inventory/inventoryitem.
LocationService is an interface for interfacing with the location endpoints of the Shopify API.
MetafieldService is an interface for interfacing with the metafield endpoints of the Shopify API.
MetafieldsService is an interface for other Shopify resources to interface with the metafield endpoints of the Shopify API.
OrderService is an interface for interfacing with the orders endpoints of the Shopify API.
PagesPageService is an interface for interacting with the pages endpoints of the Shopify API.
ProductService is an interface for interfacing with the product endpoints of the Shopify API.
RecurringApplicationChargeService is an interface for interacting with the RecurringApplicationCharge endpoints of the Shopify API.
RedirectService is an interface for interacting with the redirects endpoints of the Shopify API.
ScriptTagService is an interface for interfacing with the ScriptTag endpoints of the Shopify API.
ShopService is an interface for interfacing with the shop endpoint of the Shopify API.
SmartCollectionService is an interface for interacting with the smart collection endpoints of the Shopify API.
StorefrontAccessTokenService is an interface for interfacing with the storefront access token endpoints of the Shopify API.
ThemeService is an interface for interfacing with the themes endpoints of the Shopify API.
TransactionService is an interface for interfacing with the transactions endpoints of the Shopify API.
UsageChargeService is an interface for interacting with the UsageCharge endpoints of the Shopify API.
VariantService is an interface for interacting with the variant endpoints of the Shopify API.
WebhookService is an interface for interfacing with the webhook endpoints of the Shopify API.

# Type aliases

Option is used to configure client with options.