|
1 | | -# Consuming content locally |
| 1 | +# Consuming Content Locally |
2 | 2 |
|
3 | | -Consuming created content is essential to actually benefit from a CMS. elek.io Client and Core therefore provide multiple ways of doing so. |
| 3 | +## Overview |
4 | 4 |
|
5 | | -**Use Cases**: |
| 5 | +Once you've created content in elek.io, you probably want to consume it in your applications. elek.io Client and Core provide multiple ways to access your Project data from external applications. |
6 | 6 |
|
7 | | -- Static site generators (Next.js, Gatsby, Hugo) |
8 | | -- Custom build tools |
9 | | -- Really anything you can think of |
| 7 | +**Common Use Cases**: |
10 | 8 |
|
11 | | -## Export Project data to JSON file |
| 9 | +- **Static Site Generators**: Fetch content for Next.js, Gatsby, Astro, Hugo, etc. |
| 10 | +- **Custom Build Tools**: Integrate content into your CI/CD pipeline |
| 11 | +- **Mobile Apps**: Access content via the local API from React Native, Flutter, etc. |
| 12 | +- **Desktop Applications**: Read content from other Electron or native apps |
| 13 | +- **Documentation Sites**: Generate docs from your elek.io content |
| 14 | +- **Data Analysis**: Export content for analytics or reporting tools |
12 | 15 |
|
13 | | -elek.io Core can be installed globally via your package manager of choice: |
| 16 | +## Export Project Data to JSON Files |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +First, install elek.io Core globally via your package manager: |
14 | 21 |
|
15 | 22 | ```bash |
16 | 23 | npm install -g @elek-io/core |
17 | 24 | ``` |
18 | 25 |
|
19 | | -With the included CLI you can then run one of the following commands inside e.g. your websites source code directory: |
| 26 | +### Export Commands |
| 27 | + |
| 28 | +The built-in CLI allows you to export Project data to JSON files. Run these commands e.g. inside your website's source code directory (or any directory where you want the exported data). Alternatively integrate them into your build scripts or CI/CD pipelines to ensure your application always has the latest content: |
20 | 29 |
|
21 | 30 | ```bash |
22 | 31 | # Export all Projects content to .elek.io/projects.json |
23 | 32 | elek export |
24 | 33 |
|
25 | | -# Export one Project to .elek.io/projects.json |
26 | | -elek export ./.elek.io [projectId] |
| 34 | +# Export one Project by ID to .elek.io/projects.json |
| 35 | +elek export ./.elek.io 467e57ea-e04a-44a7-b34b-684ed3ba6f49 |
27 | 36 |
|
28 | 37 | # Export one Project to .elek.io/project-${id}.json |
29 | | -elek export ./.elek.io [projectId] |
| 38 | +elek export ./.elek.io 467e57ea-e04a-44a7-b34b-684ed3ba6f49 |
30 | 39 |
|
31 | 40 | # Export multiple Projects into separate .elek.io/project-${id}.json files |
32 | | -elek export ./.elek.io [projectId1,projectId2] --separate |
| 41 | +elek export ./.elek.io 467e57ea-e04a-44a7-b34b-684ed3ba6f49,bcffed17-4946-4336-b420-3974d9c94a43 --separate |
33 | 42 | ``` |
34 | 43 |
|
35 | | -Add the `-w` or `--watch` option to any of these commands to automatically re-export whenever a Project changes. |
| 44 | +> [!NOTE] |
| 45 | +> Replace `467e57ea-e04a-44a7-b34b-684ed3ba6f49` with your actual Project ID. You can find Project IDs in the elek.io Client UI or in your Project's `.elek.io` directory. |
| 46 | +
|
| 47 | +### Watch Mode |
36 | 48 |
|
37 | | -Then use the content inside these JSON file(s) to populate your website or application: |
| 49 | +Add the `-w` or `--watch` flag to automatically re-export whenever a Project changes: |
| 50 | + |
| 51 | +```bash |
| 52 | +elek export ./.elek.io 467e57ea-e04a-44a7-b34b-684ed3ba6f49 --watch |
| 53 | +``` |
| 54 | + |
| 55 | +This is particularly useful during development when you're actively updating content. |
| 56 | + |
| 57 | +### Using Exported JSON |
| 58 | + |
| 59 | +Once exported, you can import and use the JSON data in your application: |
38 | 60 |
|
39 | 61 | ```typescript |
40 | 62 | import * as projects from './.elek.io/projects.json' with { type: 'json' }; |
41 | 63 |
|
42 | | -const entries = projects['...'].collections['...'].entries; |
| 64 | +// Access a specific Project's Collections |
| 65 | +const myProject = projects['467e57ea-e04a-44a7-b34b-684ed3ba6f49']; |
| 66 | +const blogPosts = myProject.collections['blog-posts'].entries; |
43 | 67 |
|
44 | | -// Use the Entries |
| 68 | +// Use the Entries in your app |
| 69 | +blogPosts.forEach((post) => { |
| 70 | + console.log(post.title, post.content); |
| 71 | +}); |
45 | 72 | ``` |
46 | 73 |
|
47 | 74 | ## Local API |
48 | 75 |
|
49 | | -Enable the local API in your User Profile of elek.io Client → "Enable Local API Server". Then you can visit `http://localhost:31310/` (or your specified port) to view a rendered OpenAPI documentation and execute queries. |
| 76 | +elek.io Core also provides a local REST API server that runs on your machine, allowing applications to query your elek.io Projects for content. |
| 77 | + |
| 78 | +### Enabling the Local API |
| 79 | + |
| 80 | +**Via elek.io Client** |
| 81 | + |
| 82 | +1. Open elek.io Client |
| 83 | +2. Navigate to **User Profile** (top-right menu) |
| 84 | +3. Scroll to the **Local API** section |
| 85 | +4. Toggle **"Enable Local API Server"** |
| 86 | +5. Optionally change the port (default: `31310`) |
| 87 | +6. Click **Save** |
| 88 | + |
| 89 | +**Via elek.io Core** |
| 90 | + |
| 91 | +You can also start the local API server directly using elek.io Core CLI: |
| 92 | + |
| 93 | +```bash |
| 94 | +elek api:start 31310 |
| 95 | +``` |
| 96 | + |
| 97 | +Or programmatically in your Node.js application: |
| 98 | + |
| 99 | +```typescript |
| 100 | +import ElekIoCore from '@elek-io/core'; |
| 101 | + |
| 102 | +const core = new ElekIoCore(); |
| 103 | +core.api.start(31310); |
| 104 | +``` |
| 105 | + |
| 106 | +Once enabled, you can visit `http://localhost:31310/` (or your specified port) to view the OpenAPI documentation and execute test queries. |
| 107 | + |
| 108 | +> [!WARNING] |
| 109 | +> Make sure the port you choose is not already in use by another application. If the API fails to start, try a different port. |
50 | 110 |
|
51 | | -### Generate TS/JS API Client |
| 111 | +### Generate TypeScript/JavaScript API Client |
52 | 112 |
|
53 | | -Of course you could write an API Client yourself but for TypeScript / JavaScript applications we provide a generated API Client with validation. |
| 113 | +While you can write an API client manually, elek.io provides a code generator that creates a type-safe client with built-in validation for TypeScript and JavaScript applications. |
54 | 114 |
|
55 | | -Install elek.io Core globally via your package manager of choice: |
| 115 | +#### Generate the Client |
| 116 | + |
| 117 | +If you haven't already, install elek.io Core globally: |
56 | 118 |
|
57 | 119 | ```bash |
58 | 120 | npm install -g @elek-io/core |
59 | 121 | ``` |
60 | 122 |
|
61 | | -Then use the CLI to generate a TS or JS API Client with one of the following commands: |
| 123 | +Then generate the API client: |
62 | 124 |
|
63 | 125 | ```bash |
64 | | -# Generate TS API Client with default options in ./.elek.io/client.ts |
| 126 | +# Generate TypeScript API Client with default options in ./.elek.io/client.ts |
65 | 127 | elek generate:client |
66 | 128 |
|
67 | | -# Generate JS API Client with ESM and target ES2020 in ./.elek.io/client.ts |
| 129 | +# Generate JavaScript API Client with ESM and target ES2020 |
68 | 130 | elek generate:client ./.elek.io js esm es2020 |
69 | 131 | ``` |
70 | 132 |
|
71 | | -Then import and use the generated API Client like: |
| 133 | +#### Using the Generated Client |
| 134 | + |
| 135 | +Import and use the generated API client in your application: |
72 | 136 |
|
73 | 137 | ```typescript |
74 | 138 | import { apiClient } from './.elek.io/client.js'; |
75 | 139 |
|
76 | 140 | const client = apiClient({ |
77 | 141 | baseUrl: 'http://localhost:31310', |
78 | | - apiKey: 'abc123', // Not used for now |
| 142 | + apiKey: 'abc123', // Not currently used, reserved for future authentication |
79 | 143 | }); |
80 | 144 |
|
81 | | -const entries = |
82 | | - await client.content.v1.projects['...'].collections['...'].entries.list(); |
| 145 | +// Fetch all Entries from a specific Collection |
| 146 | +const blogPosts = |
| 147 | + await client.content.v1.projects[ |
| 148 | + '467e57ea-e04a-44a7-b34b-684ed3ba6f49' |
| 149 | + ].collections['blog-posts'].entries.list(); |
| 150 | + |
| 151 | +// Access individual Entries |
| 152 | +blogPosts.forEach((post) => { |
| 153 | + console.log(post.title, post.publishedAt); |
| 154 | +}); |
83 | 155 | ``` |
84 | 156 |
|
85 | | -### Generate API Client for other languages |
| 157 | +### Generate API Client for Other Languages |
86 | 158 |
|
87 | | -For languages other than TS/JS you can either write an API Client yourself, or use an OpenAPI generator like: |
| 159 | +For languages other than TypeScript/JavaScript, you can use any OpenAPI-compatible code generator. The local API exposes its full schema at `/openapi.json`. |
| 160 | + |
| 161 | +#### Example: Generate a Java Client |
88 | 162 |
|
89 | 163 | ```bash |
90 | 164 | npx openapi-generator-cli generate \ |
91 | 165 | -i http://localhost:31310/openapi.json \ |
92 | 166 | -g java \ |
93 | 167 | -o ./.elek.io/client |
94 | 168 | ``` |
| 169 | + |
| 170 | +#### Example: Generate a Python Client |
| 171 | + |
| 172 | +```bash |
| 173 | +npx openapi-generator-cli generate \ |
| 174 | + -i http://localhost:31310/openapi.json \ |
| 175 | + -g python \ |
| 176 | + -o ./.elek.io/client |
| 177 | +``` |
| 178 | + |
| 179 | +> [!NOTE] |
| 180 | +> Make sure the local API is running before generating the client, as the generator needs to fetch the OpenAPI schema. |
| 181 | +
|
| 182 | +For more generators and options, see the [OpenAPI Generator documentation](https://openapi-generator.tech/docs/generators). |
| 183 | + |
| 184 | +## Troubleshooting |
| 185 | + |
| 186 | +### Port Already in Use |
| 187 | + |
| 188 | +If you see an error that the port is already in use: |
| 189 | + |
| 190 | +1. Check which application is using the port: `lsof -i :31310` (macOS/Linux) or `netstat -ano | findstr :31310` (Windows) |
| 191 | +2. Either stop that application or choose a different port |
| 192 | + |
| 193 | +### API Not Starting |
| 194 | + |
| 195 | +If the local API fails to start: |
| 196 | + |
| 197 | +1. Check the elek.io Client logs for error messages |
| 198 | +2. Ensure you have the latest version of elek.io Client and Core |
| 199 | +3. Try restarting elek.io Client |
| 200 | +4. Verify no firewall is blocking the port |
| 201 | + |
| 202 | +### Generated Client Not Working |
| 203 | + |
| 204 | +If the generated API client has type errors or runtime issues: |
| 205 | + |
| 206 | +1. Regenerate the client with the latest schema |
| 207 | +2. Ensure the local API is running and accessible |
| 208 | +3. Check that your Project IDs and Collection IDs are correct |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +**Last Updated:** 2025-11-18 |
0 commit comments