|
3 | 3 | Public api assets for the fulfillmenttools platform API |
4 | 4 |
|
5 | 5 | - [Open API Specification](api.swagger.yaml) |
6 | | -- [Technical Documentation](https://docs.fulfillmenttools.com) |
| 6 | +- [Technical Documentation](https://docs.fulfillmenttools.com) |
| 7 | + |
| 8 | +## Generate API Client with HeyAPI OpenAPI Generator |
| 9 | + |
| 10 | +### 1. Obtain the OpenAPI specification |
| 11 | +Ensure you have the latest version of FFT’s `api.swagger.yaml` in your working directory. You can download it from [this link](https://raw.githubusercontent.com/fulfillmenttools/fulfillmenttools-api-reference/refs/heads/master/api.swagger.yaml). |
| 12 | +```bash |
| 13 | +wget https://raw.githubusercontent.com/fulfillmenttools/fulfillmenttools-api-reference/refs/heads/master/api.swagger.yaml |
| 14 | +``` |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +### 2. Generate the client (e.g. node / typescript) |
| 19 | +Run the generator with your preferred target language. For example, to generate a TypeScript client using the Fetch API: |
| 20 | +```bash |
| 21 | +npx @hey-api/openapi-ts -i api.swagger.yaml -o src/client |
| 22 | +``` |
| 23 | + |
| 24 | + |
| 25 | +### 3. Use the client in a project (node / typescript) |
| 26 | + |
| 27 | +1. Initialize a new node project |
| 28 | +```bash |
| 29 | +npm init -y |
| 30 | +npm install typescript ts-node node-fetch @types/node |
| 31 | +``` |
| 32 | + |
| 33 | +2. Compile and Integrate the Generated Client |
| 34 | +- Copy the `src/client` folder into your project |
| 35 | +- Add a tsconfig.json to configure TypeScript if not present: |
| 36 | +```json |
| 37 | +{ |
| 38 | + "compilerOptions": { |
| 39 | + "target": "ES2020", |
| 40 | + "module": "commonjs", |
| 41 | + "outDir": "./dist", |
| 42 | + "esModuleInterop": true, |
| 43 | + "strict": true, |
| 44 | + "skipLibCheck": true |
| 45 | + }, |
| 46 | + "include": ["./src/client", "./src"] |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +3. Call an API endpoint |
| 51 | +- Create a src/index.ts file and replace placeholders with your tenant information and credentials |
| 52 | +```typescript |
| 53 | +import { type ClientOptions, getAllUsers } from "./client"; |
| 54 | +import { createConfig } from "./client/client"; |
| 55 | +import { client } from "./client/client.gen"; |
| 56 | + |
| 57 | +const options = { |
| 58 | + baseUrl: 'https://<tenant>.api.fulfillmenttools.com', |
| 59 | + email: '<email>', |
| 60 | + password: '<password>', |
| 61 | + apiKey: '<api-key>' |
| 62 | +} |
| 63 | + |
| 64 | +async function getJwtToken(): Promise<string> { |
| 65 | + const url = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${options.apiKey}`; |
| 66 | + const res = await fetch(url, { |
| 67 | + method: 'POST', |
| 68 | + headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, |
| 69 | + body: JSON.stringify({ email: options.email, password: options.password, returnSecureToken: true,}), |
| 70 | + }); |
| 71 | + |
| 72 | + if (!res.ok) { |
| 73 | + const text = await res.text(); |
| 74 | + console.error(text); |
| 75 | + throw new Error('Error obtaining firebase token, please check credentials.'); |
| 76 | + } |
| 77 | + return (await res.json()).idToken; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +async function run() { |
| 82 | + try { |
| 83 | + client.setConfig(createConfig<ClientOptions>({ |
| 84 | + baseUrl: options.baseUrl, |
| 85 | + headers: { Authorization: `Bearer ${await getJwtToken()}` }, |
| 86 | + })); |
| 87 | + |
| 88 | + const result = await getAllUsers(); |
| 89 | + console.log(result.data); |
| 90 | + } catch (error) { |
| 91 | + console.error('Request failed:', error); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +run(); |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +4. Run the Code |
| 100 | +```bash |
| 101 | +npx ts-node src/index.ts |
| 102 | +``` |
0 commit comments