Setup Extension on Fynd Partners
Expose your dev environment
To complete OAuth authorization process (seller install approval) in Fynd Platform, your app would require a public https url. Since http://localhost:3000
isn't public; therefore, you may use ngrok to create a secure tunnel from public internet to the local server.
Run the below commands to install and run ngrok in a separate terminal.
npm install ngrok -g
ngrok http 3000
Note down and copy the public URL you get after running the above commands.
Get API_KEY and API_SECRET of your extension
You need API Key and API Secret to authenticate your app on Fynd Platform. You can obtain the API credentials after creating an extension on Fynd Partners Panel.
If you don't have an account on Fynd Partners, click here to create one. You may read the documentation on creating a new Partner account.
Steps to get API credentials
-
Log in to your Fynd Partners account, and go to the Extensions section.
Figure 2: Fynd Partner Panel
-
Choose the Private option to create an extension.
Figure 3: Choosing Private Extension
-
Fill all the necessary details. Check Private Extension to know more about the details.
-
Next, click Create to save the configuration.
Figure 4: Saving The Configuration
-
Click the extension you created.
Figure 5: Selecting Your Extension
-
Note the API Key and API Secret. Ensure you don't reveal it to anyone.
Figure 6: API Credentials
Add your extension API_KEY and API_SECRET in your project
Best practice is to store and access API credentials using environment variables. Do not commit it with your code. To achieve this, use a .env
file and take help of the dotenv
package to access (read) them in your project. Copy your api_key and api_secret from Fynd Partners Panel and paste them in the .env
file as shown below.
EXTENSION_API_KEY="your-api-key"
EXTENSION_API_SECRET="your-api-secret"
EXTENSION_URL="your-ngrok-url"
You may check the available guide to check how to integrate the credentials in your project.
Setup an Express Extension FDK
-
Install the below library to set up an extension FDK.
npm i dotenv axios
npm i git+https://github.com/gofynd/fdk-client-javascript.git --save
npm i git+https://github.com/gofynd/fdk-extension-javascript.git#v0.2.1 --save -
Initialize the fdk extension library as shown in the code below.
noteWe have used MemoryStorage for a quick setup. However, for production-grade deployment, use RedisStorage.
const dotenv = require('dotenv');
'use strict';
const { setupFdk } = require("fdk-extension-javascript/express");
const { MemoryStorage } = require("fdk-extension-javascript/express/storage");
const extensionHandler = require("./extension.handler");
const redis = require("./connections/redis");
let fdkExtension = setupFdk({
api_key: process.env.EXTENSION_API_KEY,
api_secret: process.env.EXTENSION_API_SECRET,
base_url: process.env.EXTENSION_URL,
scopes: ["company/product"],
callbacks: {
auth: async function(req) {
// Writee you code here to return initial launch url after suth process complete
return req.extension.base_url;
},
uninstall: async function(req) {
// Write your code here to cleanup data related to extension
// If task is time taking then process it async on other process.
}
},
storage: new MemoryStorage("prefix_key"),
access_mode: "offline",
// cluster: "https://api.fyndx0.de" // this is optional by default it points to prod.
});
module.exports = fdkExtension; -
Now attach this fdkExtension to the Express app server.
'use strict';
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const path = require("path");
const dotenv = require('dotenv');
dotenv.config();
const app = express();
app.use(cookieParser("ext.session"));
app.use(bodyParser.json({
limit: '2mb'
}));
app.use('/_healthz', (req, res, next) => {
res.json({
"ok": "ok"
});
});
app.use("/", fdkExtension.fdkHandler);
app.use(express.static(path.join(__dirname, "./dist")));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, "./dist/index.html"));
});
module.exports = app;