In this step, you connect your app to the BigCommerce ecosystem embedding it into Draft Apps.
To authenticate and use your app with the BigCommerce API, install BigCommerce’s in-house node-bigcommerce module (GitHub).
You can view a list of all the tested package versions in the package.json file on the Step 2 branch of this sample app’s repo.
In the root directory of your app, create a lib folder.
In the lib folder, create an auth.ts file.
Open the auth.ts file and add the BigCommerce import at the top of the file.
The bigcommerceSigned function is called when loading or uninstalling the application.
getBCAuth function.You use the authorize method for the /auth API endpoint which gets called when you install or update the app. The authorize method retrieves your permanent access token and returns it in the data object.
getBCVerify function.The verifyJWT method employs the signed_payload_jwt query parameter to authenticate requests. You use the verifyJWT method for both /load and /uninstall API endpoints. To learn more about the OAuth flow, see Single-Click App OAuth Flow.
You can view auth.ts (GitHub).
Next.js maps all APIs that are part of the Next.js application to the /api/* route. You can take advantage of it by creating a sub-directory within the pages folder called api. This signals Next.js to treat files within /pages/api as API endpoints and automatically handle their routing. To learn more about Next.js API routes, see API Routes.
In the pages folder, create an api folder.
Open the api folder and add the following files: auth.ts, load.ts, and uninstall.ts.
Open the auth.ts file and add the logic to authorize the app. You can view auth.ts (GitHub).
The /auth endpoint gets called when installing the app. Launching (loading) or uninstalling the app will not call the /auth endpoint.
load.ts file and add the logic to sign the user in when the app calls the /load endpoint (when launching the app). You can view load.ts (GitHub).uninstall.ts file and add the logic to remove a user who has uninstalled the application from their BigCommerce account. You can view uninstall.ts (GitHub).To connect your sample app to BigCommerce, you need a publicly accessible URL. To add network access while still in the development phase, you can use ngrok, a free tool that lets you expose local servers like localhost:3000 to the public internet over secure tunnels.
You can obtain your authtoken by going to https://dashboard.ngrok.com/get-started/your-authtoken.

Although you can use the ngrok npm package without creating an account, any unauthenticated tunnels you create will expire after two hours. For the best development experience, create a free ngrok account, find your ngrok authtoken, and add the authtoken to your global ngrok configuration.
To register an app, you need a BigCommerce store. If you do not have a BigCommerce store, visit the BigCommerce Pricing page to start a free trial.

Enter app details at the prompts. Because you are building a sample app, you can name it anything you want. Production-level apps should meet the general requirements outlined in Approval Requirements.
Click Technical.
Under App Features, select STORE OWNER for Multiple Users and SINGLE-CLICK for App Type.
To fill out Callback URLs, retrieve the public HTTPS URL of your ngrok tunnel.

Avoid using the unsecured HTTP URL to prevent security policy errors.
/api/auth./api/load./api/uninstall.
Next.js maps all APIs that are part of the Next.js application to the /api/* route. To learn more about Next.js API routes, see API Routes.

Click Update & Close.
Select Confirm Update.
You should see your app listed under My Apps in your Developer Portal account.
It is best practice to declare environment variables in the .env environment file. You use the .env file to store your Client ID and Client Secret Key.
Next.js comes pre-equipped to handle environment variables. It loads environment variables from .env.local into process.env, allowing you to use them in Next.js data fetching and API routes. To learn more, see Next.js Environment Variables.
Create an .env file in the root directory of your app.
Add the app’s credentials and auth callback placeholders to the .env file.
Never share sensitive data such as API keys and passwords publicly. Avoid committing them to your repository.
.env file.AUTH_CALLBACK in .env with your {ngrok_url}. Because env variables are loaded on build, make sure to save your changes.If ngrok stops working or your ngrok session expires, restart the tunnel to get the new {ngrok_url} and update the callback URLs in the Developer Portal and the AUTH_CALLBACK in the .env file.

Click the app’s name.
Click Install.
Click Confirm.
You should see your app embedded in the BigCommerce platform.
