Tutorial: Etsy Open API v3 (ruby)
Nothing better to test a blog’s capabilities than making some personal notes public. I have been hacking around Etsy APIs to extract some sales data from their backend and send it to some custom spreadsheets.
Sure I could have used the limited CSV export capabilities from their admin portal, but where is the fun in that? I needed to see how much I could get done with their APIs.
Etsy documentation
Some documentation exists and there is an API reference section which gives a good idea of what is available. Worth having a look before considering the advanced engineering path.
One thing that I’ll mention upfront is that you need to create an application in the developer dashboard to get a valid API key. It can take a few days for an application to be approved by their team and the API key will not work until then. It is worth it to create an app ASAP so the review process does not become a blocker.
Ruby implementation
I’m a Ruby dev, so that’s what you are going to get today. Since OpenAPI is all about supporting many languages, odds are it should also work for you the same way if you use something else.
OpenAPI Generator
I don’t have any experience running an OpenAPI server, but I appreciate consuming from one.
Inspecting the network calls performed by their API reference page, there is an XHR to https://www.etsy.com/openapi/generated/oas/3.0.0.json.
I installed openapi-generator
:
And ran the following to create my EtsyApi
client library:
It generates a gem, so do not forget to add gem "etsy_api", path: "vendor/etsy_api"
to your Gemfile
.
And that’s it.
Etsy Authentication
Obtaining Oauth2 tokens (once)
This is the part where I had a lot of trial and error. Some of it was my limited understanding of Etsy API, and the other half was remembering how to configure Oauth2 in OpenAPI.
Authentication - Etsy Open API v3
I only needed to run some scripts server-side. The Oauth2 code demonstrated here is limited to self-use, not requesting through web application users.
Preparation:
A callback URL (eg.: “https://localhost:8080/oauth2/callback”) needs to be added to your Etsy application. Without a valid callback URL, your Oauth2 won’t go through.
Note: I use dotenv for my environment variables. It works well, be sure to add the .env
to your .gitignore
.
Authentication - Etsy Open API v3: Step 1 request an authorization code
I have been using gem "oauth2"
so I didn’t have to worry too much about the implementation details.
The next steps are manual, but it is a one-time thing:
- Take the URL and punch it in a browser. The following page will open:
-
Grant permission. The redirect URL should look like:
https://localhost:8080/oauth2/callback?code=kq7ORZrP...&state=...
. -
Validate the “state” parameter matches the
ETSY_CLIENT_STATE
environment variable. -
Assign the “code” from the query parameters in a local variable.
Authentication - Etsy Open API v3: Step 3 request an access token
Configuring Oauth2 tokens
Usage
Once the complex part is complete, the following should work:
Technical support
Through my work, I had some questions. I feel it is worth sending some kudos when due. The https://github.com/etsy/open-api/ repository was a nice touch where developers can interact with Etsy engineers. Developers can ask questions, provide feedback or file bug reports.
Conclusion
I hope this test post was of help to you and made the road to API integration as fast and boring as possible.