Quick Start Tutorial
This tutorial walks you through building a very simple Node.js application from start to finish. By the end, you will have a working application with a valid OAuth 2.0 access token to make requests to any scoped endpoint.
#
Initial setupTo get setup, follow these general steps:
- To begin, you’ll need a development environment with Node.js installed. If you’re unfamiliar with this setup, follow these official instructions.
- In your environment, create a directory called
etsyapp
and a new file in that directory calledhello.js
: - Write a one line JavaScript program to print a string literal to the console:
- Save
hello.js
- Open a terminal window, navigate to the
etsyapp
directory and type the following command:
If your node and development environment is set up correctly, the terminal responds with the message "Hello, world!"
#
Get your API keyTo complete any request to the Etsy Open API v3, you require an Etsy application ID, also know as an API key. Etsy issues API keys upon request to develop an application for your Etsy account.
#
Register a new applicationIf you don’t have an existing Etsy application, you’ll need to create one. If you have one already, you can skip to the next section.
Open a web browser and navigate to https://www.etsy.com/developers/register.
At a minimum, enter information into the following fields:
- Name: the name of your app.
- Description: A description of the app that you can recognize and use to distinguish it from other apps.
- Who will be the users of this application? Choose the option that best matches your use case.
- Is your application commercial? Select "no". You can change this setting later for a commercial app.
Complete the captcha identity verification and click "Read Terms and Create App".
#
Find the keystring for your application- Navigate to Your Apps on Etsy’s developer page.
- Click on the plus sign next to "SEE API KEY DETAILS" for the application you want to use in this tutorial. Copy down your Etsy App API Key keystring.
#
Start with a simple Express server applicationThe OAuth2 Authorization Code flow requires a callback URL to which to send the authorization code, which in this case maps to your application. For a server-side app built with Node, this means Node must be running as a server and the callback URL must map to a specific resource on that server. To keep this example simple, we use the Express package for Node and build a basic web service to handle the OAuth2 Authorization Code response.
To build a simple web server in Node, perform the following steps:
- We will be using several npm packages for this tutorial. Start by creating a new file in your
etsyapp
directory calledpackage.json
. Paste the following code in the file and save it.
From your terminal, run
npm install
.Create a new file called server.js in your etsyapp directory. For example, you can type the following command in the terminal window and press enter:
Open
server.js
for editing and write code to start a simple web server on port 3003. For example, if your host URL is localhost:Save
server.js
Open a terminal window and type the following command:
If you successfully started the web server, your console log should read:
And if you open http://localhost:3003 in a web browser on the same machine or any connected machine, you should see the message "Hello, world!" on the web page.
#
Test your API keyThere are a limited set of Open API v3 endpoints that do not require an OAuth token, so you can test your API key before requesting an access token. Test your API key using the following procedure:
- Open
server.js
again. - Write code to make a GET request to the
ping
endpoint — which does not require authentication scopes — and print the response to the console. For example, if your API keystring were1aa2bb33c44d55eeeeee6fff
, then your updated code might look like the following:
- Save
server.js
- Open a terminal window and type the following command:
Open http://localhost:3003/ping in a web browser on the same machine or any connected machine. If your API keystring is working properly, you should see a JSON array that includes the application ID for the application associated with this keystring. The response should look like this:
#
Create the client landing pageWe create an entry point to our application by creating a "views" directory — which Express exposes with middleware — and adding a client landing page that makes an OAuth2 request. To create the client landing page, perform the following procedure:
Before you begin, you will need the following authentication parameters:
- a redirect URI on your web server, such as http://localhost:3003/oauth/redirect, which you will setup as the '/oauth/redirect' route in the next section
- a state string, which can be any non-empty, non-whitespace string, but which you must use in all requests in the same Authorization code flow, for example "superstring"
- a PKCE code challenge generated from the code verifier, which Etsy requires later in the authorization code flow
In your etsyapp directory, create a new directory named "views".
Navigate to the
/views
directory and create a new file called "index.hbs".Open index.hbs for editing and code a simple HTML page with an OAuth request. For example, to request an authorization code for an Etsy app requiring an
email_r
scope with an App API Key keystring of1aa2bb33c44d55eeeeee6fff
, you can create a page with one link like the following:
This link in this template has every element required for an OAuth connect request to Etsy, as detailed in the Authentication topic on this site. If you need help generating the state and code challenge values, follow the instructions below.
#
Generate the PKCE code challengeThere are a variety ways to generate the state, code challenge, and code verifier values, but we thought it would be helpful to share an example.
- To start, create a new file called
code-generator.js
. - Paste in the following code.
- Save the file and run
node code-generator.js
from a terminal prompt. Here’s an example of a successful response in the terminal.
redirect_uri
route#
Implement the The URI string for the client landing page above contains a redirect_uri
"http://localhost:3003/oauth/redirect", which is a local address to which the OAuth server delivers the authorization code. In Node, this redirect_uri
must be a route to a resource to receive the authentication code and use it to request an OAuth token. Use the following procedure to create the redirect route:
- Navigate to your /etsyapp directory and open
server.js
in your editor. - Add code for the "oauth/redirect" route to make a POST request to
https://api.etsy.com/v3/public/oauth/token
with the authentication code gathered from the request , as shown below:
- Go back to your terminal prompt and run
node server.js
. Then visit http://localhost:3003. You should see the full JSON payload returned from a successful OAuth flow.
tip
If you see an error after clicking the “Authenticate with Etsy” button, you might need to add your redirect URI.
#
Display a response from a scoped endpointTo finish the tutorial, we will use the generated access token to make an authenticated request to a scoped endpoint. Follow the steps below to create one last route in your Express application.
- Create a "views/welcome.hbs" file. This template will be used to offer a welcome message to the authenticated user. Copy the following code into the file.
- Write code in our new route handler to request user information from the Open API v3 getUser endpoint. This requires an access token with
email_r
scope, which was established in the initial authorization code request. For example, this will welcome the authenticated user by their first name:
- Go back to your terminal prompt and run
node server.js
. Then visit http://localhost:3003. If thecode_challenge
value in your index.hbs file is still valid, you should be able to go through the authentication flow and find yourself at the/welcome
page with the following message:
#
Handling errorsWhile thorough error handling is out of scope for this tutorial, here is a simple way to see more of the error response with fetch
. When response.ok
returns false
, you can access the error response with the following code:
If you followed every step of this tutorial, you likely ran into an error when implementing the welcome page as your code value had been used before. If you implement the error handling above, errorData
would log the following value:
#
Wrap upThat’s where we will wrap up our quickstart guide. Here are a few places you could turn next:
- Requesting a Refresh OAuth Token
- API Reference
- Explore one of our other tutorials