Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork13
🚚 GitHub action for handling authenticated API requests, allowing you to save the data from the request into your workspace as an environment variable and a .json file.
License
JamesIves/fetch-api-data-action
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
ThisGitHub Action will handle authenticated API requests for you, allowing you to save the data from the request into your workspace as an environment variable and a file. Using this action will allow you to save data from these queries on a schedule so they can be used in a static page without exposing your API credentials. You can read more about the inspiration for this actionhere.
Maintenance of this project is made possible by all thecontributors andsponsors. If you'd like to sponsor this project and have your avatar or company logo appear belowclick here. 💖
You can include the action in your workflow to trigger on any event thatGitHub actions supports. You'll need to provide the action with the endpoint you'd like to request along withany configuration options asstringified JSON.
name:Refresh Feedon:[push]jobs:refresh-feed:runs-on:ubuntu-lateststeps: -name:Fetch API Data 📦uses:JamesIves/fetch-api-data-action@v2with:endpoint:https://example.comconfiguration:'{ "method": "GET", "headers": {"Authorization": "Bearer ${{ secrets.API_TOKEN }}"} }'
Once the action has run the requested data will be exported into thefetch-api-data environment variable and will also be available as a.json file in your workspace located by default in thefetch-api-data-action/data.json directory. If you need something other than.json file please refer to theformat parameter.
You can combine the use of this with theGitHub Pages Deploy Action to trigger scheduled updates to a feed on your website.
You can view a full example of this here.
In one workflow you can fetch data from an API on a schedule and push it to yourmain branch.
name:Refresh Feedon:schedule: -cron:10 15 * * 0-6permissions:contents:writejobs:refresh-feed:runs-on:ubuntu-lateststeps: -name:Checkout 🛎️uses:actions/checkout@v2with:persist-credentials:false -name:Fetch API Data 📦uses:JamesIves/fetch-api-data-action@v2with:endpoint:https://example.comconfiguration:'{ "method": "GET", "headers": {"Authorization": "Bearer ${{ secrets.API_TOKEN }}"} }' -name:Build and Deploy 🚀uses:JamesIves/github-pages-deploy-action@v4with:branch:main# Pushes the updates to the main branch.folder:fetch-api-data-action# The location of the data.json file saved by the Fetch API Data action.target-folder:data# Saves the data into the 'data' directory on the main branch.
In another workflow you can then build and deploy your page.
name:Build and Deployon:schedule: -cron:10 16 * * 0-6permissions:contents:writejobs:build-and-deploy:runs-on:ubuntu-lateststeps: -name:Checkout 🛎️uses:actions/checkout@v2with:persist-credentials:false -name:Install 🔧run:| npm install npm run-script build -name:Build and Deploy 🚀uses:JamesIves/github-pages-deploy-action@v4with:branch:gh-pagesfolder:build
In your project you can import the JSON file and make it part of your build script. This way your site will re-build and deploy whenever refreshed data has been fetched from the server.
If you'd like to use the functionality provided by this action in your own action you can eithercreate a composite action, or you can install it usingyarn ornpm by running the following commands. It's available on both thenpm andGitHub registry.
yarn add @jamesives/fetch-api-data-actionIt can then be imported into your project like so.
importrun,{retrieveData,generateExport,ActionInterface}from'@jamesives/fetch-api-data-action'
Calling the functions directly will require you to pass in an object containing the variables found in theconfiguration section.
importrunfrom'@jamesives/fetch-api-data-action'run({endpoint:'https://example.com',configuration:JSON.stringify({method:'GET',headers:{Authorization:`Bearer${process.env['TOKEN']}`}})})
Thewith portion of the workflowmust be configured before the action will work. You can add these in thewith section found in the examples above. Anysecrets must be referenced using the bracket syntax and stored in the GitHub repositoriesSettings/Secrets menu. You can learn more about setting environment variables with GitHub actionshere.
The following configuration options should be set.
| Key | Value Information | Type | Required |
|---|---|---|---|
endpoint | The URL of the endpoint you'd like to retrieve data from. For example:https://example.com/data. If noconfiguration is provided then the default request method will beGET. | with | Yes |
configuration | Any applicable configuration settings that should be set such as authentication tokens. You can reference secrets using the${{ secrets.secret_name }} syntax, or you can reference data returned from thetoken-endpoint request using the triple bracket syntax:{{{ data.access_token }}}. For more information refer to theToken Request part of the readme and theFetch API documentation. | secrets / with | No |
| Key | Value Information | Type | Required |
|---|---|---|---|
token-endpoint | If theendpoint API requires you to make a request to get an access token prior to fetching data you can perform this task by specifying a token endpoint. Any data returned from the token end can be referenced in theconfiguration variable using the triple bracket syntax:{{{ access_token }}}. For more information refer to theToken Request part of the readme; | with | No |
token-configuration | Any applicable configuration settings that should be set such as authentication tokens. You can reference secrets using the${{ secrets.secret_name }} syntax. For more information refer to theFetch API documentation. | secrets / with | No |
retry | If you're working with an intermittent API you can toggle this option totrue. Doing so will make the action try the request 3 times at random intervals before failing. | with | No |
save-location | By default the save location of the file isfetch-api-data-action/data.json, if you'd like to override the directory you can do so by specifying a new one with this variable. | with | No |
save-name | You can override the name of the exported.json file by specifying a new one here. You shouldnot include the file extension in your name. | ||
variable-name | Adjusts the name of the environment variable the action generates so long asset-output istrue. Defaults tofetchApiData. | with | No |
set-output | Determines if the returned data should be saved as an environment variable or not. This field defaults totrue, but depending on your API response length you may need to toggle this. Iftrue, an environment variable and action step output namedfetchApiData will be created. | with | No |
format | Allows you to modify the extension of the file saved from the API response, for example you can set this field tojson ortxt. This field defaults tojson. | with | No |
encoding | Allows you to modify the encoding of the file saved from the API response, for example you can set this field toutf8 orhex. This field defaults toutf8. Choose fromascii,utf8,utf-8,utf16le,ucs2,ucs-2,base64,latin1,binary orhex. | with | No |
debug | If set totrue the action will log the API responses it receives in the terminal. | with | No |
If you need to make a request to another endpoint in order to request an access token or something similar you can do so using thetoken-endpoint andtoken-configuration parameters. You can then use the returned token in yourconfiguration variable using the triple syntax like so{{{ tokens.access_token }}}. You can find an example of this below.
name:Refresh Feedon:[push]jobs:refresh-feed:runs-on:ubuntu-lateststeps: -name:Fetch API Data 📦uses:JamesIves/fetch-api-data-action@v2with:# The token endpoint is requested first. This retrieves the access token for the other endpoint.token-endpoint:https://example.com/auth/token# The configuration contains secrets held in the Settings/Secrets menu of the repository.token-configuration:'{ "method": "POST", "body": {"client_id": "${{ secrets.client_id }}", "client_secret": "${{ secrets.client_secret }}"} }'# Once the token endpoint has fetched then this endpoint is requested.endpoint:https://example.com/data# The bearer token here is returned from the TOKEN_ENDPOINT call. The returned data looks like so: {data: {access_token: '123'}}, meaning it can be accessed using the triple bracket syntax.configuration:'{ "method": "GET", "headers": {"Authorization": "Bearer {{{ data.access_token }}}"} }'
About
🚚 GitHub action for handling authenticated API requests, allowing you to save the data from the request into your workspace as an environment variable and a .json file.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.






















