Skip to main content

Fetch Department Data

Fetch the list of company departments using the fetch method:

Prerequisites

  • Valid Fynd credentials: company_id, apiKey, and apiSecret.
  • A token generated via the client_credentials flow.

Endpoint


Link:

## Implementation Steps

### 1. Set Up Configuration
Set up your Fynd API configuration using the credentials.

```js
import { PlatformConfig } from "@gofynd/fdk-client-javascript";

const platformConfig = new PlatformConfig({
companyId: "8845", // Replace with your actual company ID
apiKey: "1234b6ab7332800685f57f4d", // Replace with your API key
apiSecret: "A2BabcdXGNpVaE", // Replace with your API secret
domain: "https://api.fynd.com",
});
note

Refer to Fynd Commerce documentation.

2. Obtain OAuth Access Token

const tokenObj = await platformConfig.oauthClient.getAccesstokenObj({
grant_type: "client_credentials",
});

const token = tokenObj.access_token;

3. cURL Request

Replace placeholders with your values.

async function fetchCompanyDepartments() {
try {
const response = await fetch(`https://api.fynd.com/service/platform/catalog/v1.0/company/${platformConfig.companyId}/departments/`, {
method: 'GET',
headers: {
'Authorization': `Bearer Abc1NGI1Y1I1MzMyODAwNjg1AjU1AjAkOkszAnh1cGRyWEdOaBCDE`,
},
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const departmentsData = await response.json();
console.log(departmentsData);

return departmentsData;
} catch (error) {
console.error('Error fetching company departments:', error);
}
}

fetchCompanyDepartments();

Final Output

import { PlatformConfig } from "@gofynd/fdk-client-javascript";

const platformConfig = new PlatformConfig({
companyId: "8845", // Replace with your actual company ID
apiKey: "1234b6ab7332800685f57f4d", // Replace with your API key
apiSecret: "A2BabcdXGNpVaE", // Replace with your API secret
domain: "https://api.fynd.com",
});

// Async function to obtain token and fetch company departments
async function fetchCompanyDepartments() {
try {
// Obtain access token
const tokenObj = await platformConfig.oauthClient.getAccesstokenObj({
grant_type: "client_credentials",
});

const token = tokenObj.access_token;

const response = await fetch(`https://api.fynd.com/service/platform/catalog/v1.0/company/${platformConfig.companyId}/departments/`, {
method: 'GET',
headers: {
'Authorization': `Bearer Abc1NGI1Y1I1MzMyODAwNjg1AjU1AjAkOkszAnh1cGRyWEdOaBCDE`,
},
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const departmentData = await response.json();
console.log(departmentData);

return departmentData;
} catch (error) {
console.error('Error fetching company departments:', error);
}
}

// Example usage:
fetchCompanyDepartments();

Response

{
page: {
current: 1,
type: 'number',
size: 20,
has_previous: false,
has_next: true,
item_total: 40
},
items: [
{
uid: 32,
synonyms: [],
created_on: '2024-03-21T05:39:26.653000',
slug: 'led-lights',
_id: '12abc12e12d1234b82df57b5',
modified_by: [Object],
logo: 'https://cdn.fynd.com/v2/falling-surf-7c8bb8/fyprod/wrkr/department/pictures/square-logo/original/N_o_QnKMX-department.png',
name: 'LED Lights',
is_active: true,
priority_order: 123247598,
created_by: [Object],
modified_on: '2024-08-23T09:24:09.311000',
id: '12abc12e12d1234b82df57b5'
},
{
uid: 31,
synonyms: [],
created_on: '2024-03-19T10:36:42.006000',
slug: 'clothing-material',
_id: '12abc12e12d1234b82df57b5',
modified_by: [Object],
logo: 'https://cdn.fynd.com/v2/falling-surf-7c8bb8/fyprod/wrkr/department/pictures/square-logo/original/N_o_QnKMX-department.png',
name: 'Clothing Material',
is_active: true,
priority_order: 12344,
created_by: [Object],
modified_on: '2024-09-01T11:31:08.866000',
id: '12abc12e12d1234b82df57b5'
},
]
}

Was this section helpful?