Giter Site home page Giter Site logo

gopify's Introduction

Gopify

Go Reference

Gopify is a simple package for developing Shopify applications in Go.

Table of Contents

Usage

Oauth

When developing a public or custom Shopify application you need to get an access token using oauth to use Shopify APIs.

Start oauth process

The first thing you need to do to use this package is to create a Gopify instance like the following:

app := &gopify.Gopify{
		ApiKey:      "key",
		ApiSecret:   "secret",
		RedirectUrl: "https://example.com/auth/callback",
		Scopes:      []string{"read_products","read_orders"},
	}

Add a http handler to trigger the oauth process.

func startOauth(w http.ResponseWriter, r *http.Request) {
    shopName := r.URL.Query().Get("shop")
    authUrl := app.AuthorizationUrl(shopName, "unique token")
    http.Redirect(w, r, authUrl, http.StatusFound)
}

Oauth callback

After Shopify authenticates your app, it will send a request to the redirect url that you provided to gopify.Gopify{} above. Now you can obtain an access token using AccessToken method.

func oauthCallback(w http.ResponseWriter, r *http.Request) {
	shopName := r.URL.Query().Get("shop")
	code := r.URL.Query().Get("code")
	token, err := app.AccessToken(shopName, code)

	// Do something with the token, like querying shopify API.
	...

	// redirect to your application home page
	http.Redirect(w, r, "app url", http.StatusFound)
}

API calls

We can make calls to both Shopify APIs, REST and Graphql using the Client object provided by this package.

client := gopify.NewClient("example.myshopify.com", "access token")

We can can also pass other options to NewClient like the API version, http timeout, ...

// We can use WithVersion to specify which API version
client := gopify.NewClient("example.myshopify.com", "access token", gopify.WithVersion("2021-10"))

// Use WithTimeout to set a custom http timeout instead 10 seconds
client := gopify.NewClient("example.myshopify.com", "access token", WithTimeout(20))

REST

// Perform a Get request
products, err := client.Get("products.json")

// Perform a Post request
_, err := client.Post("products.json", gopify.Body{})

Graphql

To send a Graphql query, we use the Graphql method defined in the api Client type.

query := `
	{
      products (first: 10) {
        edges {
          node {
            id
            title
          }
        }
      }
    }
`
// the second parameter is for query variables, here we pass nil because we don't have any variables
products, nil := client.Graphql(query, nil)

Rate limiting

Shopify APIs are rate limited, so if that happens you can use the WithRetry option to specify how many times to retry a request.

// retry the request 10 times when hit the rate limit
client := gopify.NewClient("example.myshopify.com", "access token", WithRetry(10))

Session tokens

If you are building an embedded Shopify app then you need to authenticate your app with session tokens.

This package provides you with facilities for decoding a session token and extracting its payload, and also a way to verify the authenticity of the token.

// decode the token 
payload, err := app.DecodeSessionToken("token")

// verify the signature of the token
err := app.VerifyTokenSignature("token")

There is also a higher level way to verify the authenticity of token using the VerifyToken http middleware.

Verify a Shopify request

To verify the authenticity of the request from Shopify we can verify the signature of a hmac parameter included in every request from shopify using VerifyRequest http middleware.

Verify a webhook

To verify that a webhook request is from Shopify we can use VerifyWebhook function.

gopify's People

Contributors

oussama4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.