Ashby HQ
To obtain the ASHBY_API_KEY, follow these steps:
- Acquire your username and password.
- Use Base64 Decode to encode your credentials in the format
USERNAME:PASSWORD.
In the API folder
import fetch from "node-fetch";
export default async function handler(req, res) {
const jobUrl = `https://api.ashbyhq.com/job.list`;
const locationUrl = `https://api.ashbyhq.com/location.list`;
const departmentUrl = `https://api.ashbyhq.com/department.list`;
const options = {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${process.env.ASHBY_API_KEY}`,
},
};
const jobsResponse = await fetch(jobUrl, {
...options,
body: JSON.stringify({ status: ["Open"] }),
});
const jobs = await jobsResponse.json();
const locationsResponse = await fetch(locationUrl, {
...options,
body: JSON.stringify({ includeArchived: false }),
});
const locations = await locationsResponse.json();
const departmentsResponse = await fetch(departmentUrl, {
...options,
body: JSON.stringify({ includeArchived: false }),
});
const departments = await departmentsResponse.json();
const data = {
jobs: [...jobs.results],
locations: [...locations.results],
departments: [...departments.results],
};
res.status(201).json(data);
}
In the component
useEffect(() => {
fetch("/api/careers")
.then((response) => response.json())
.then((data) => {
const mapped_data = data.jobs
.filter((job) => job.jobPostingIds.length > 0)
.map((job) => {
const department = data.departments.find(
(el) => el.id === job.departmentId
);
const loc = data.locations.find((el) => el.id === job.locationId);
return { ...job, department: department.name, location: loc.name };
});
const locationFilter = ["All"];
const departmentFilter = ["All"];
mapped_data?.map((job) => {
if (!locationFilter.includes(job.location))
locationFilter.push(job.location);
if (!departmentFilter.includes(job.department))
departmentFilter.push(job.department);
return null;
});
setLocations([...locationFilter]);
setDepartments([...departmentFilter]);
setJobs(mapped_data);
})
.catch((err) => console.error(err));
}, []);