{"id":17711140,"url":"https://github.com/deejay-hub/openai-api-key-mgmt","last_synced_at":"2026-01-11T05:38:12.326Z","repository":{"id":226512167,"uuid":"753912311","full_name":"deejay-hub/openai-api-key-mgmt","owner":"deejay-hub","description":"Flex gateway policy to add OpenAI Key into header","archived":false,"fork":false,"pushed_at":"2024-02-09T11:08:53.000Z","size":625,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-06T13:49:35.304Z","etag":null,"topics":["flexgateway","flexgateway-custom-policy","mulesoft"],"latest_commit_sha":null,"homepage":"","language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deejay-hub.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-07T02:45:42.000Z","updated_at":"2024-05-09T15:35:52.000Z","dependencies_parsed_at":"2024-03-08T01:26:09.731Z","dependency_job_id":"14b77777-02b9-4bc1-b41a-00b49dab3590","html_url":"https://github.com/deejay-hub/openai-api-key-mgmt","commit_stats":null,"previous_names":["deejay-hub/openai-api-key-mgmt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deejay-hub%2Fopenai-api-key-mgmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deejay-hub%2Fopenai-api-key-mgmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deejay-hub%2Fopenai-api-key-mgmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deejay-hub%2Fopenai-api-key-mgmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deejay-hub","download_url":"https://codeload.github.com/deejay-hub/openai-api-key-mgmt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246443864,"owners_count":20778309,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flexgateway","flexgateway-custom-policy","mulesoft"],"created_at":"2024-10-25T07:44:39.717Z","updated_at":"2026-01-11T05:38:12.302Z","avatar_url":"https://github.com/deejay-hub.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\t\u003cimg\n\twidth=\"80\"\n\tsrc=\"/images/mulesoft-logo.png\"\u003e\n\t\u003ch1\u003eOpenAI API Key Management Policy\u003c/h1\u003e\n\u003c/div\u003e\n\n\u003ch4 align=\"center\"\u003e\n\t\u003ca href=\"#overview\"\u003eOverview\u003c/a\u003e |\n\t\u003ca href=\"#try-it\"\u003eTry It\u003c/a\u003e |\n  \u003ca href=\"#make-command-reference\"\u003eMake Reference\u003c/a\u003e\n\u003c/h4\u003e\n\n## Overview\n\nThis policy was created with the Flex Gateway Policy Development Kit (PDK). To find the complete PDK documentation, see [PDK Overview](https://docs.mulesoft.com/pdk/latest/policies-pdk-overview) on the MuleSoft documentation site.\n\nThe component has the following properties\n\n| Property                        | Description                              | Type                                   |\n| ------------------------------- | ---------------------------------------- | -------------------------------------- |\n| `openai-api-key`                | Allows you to input an OpenAI API Key    | String    \n\n\n### Example\n\nWhen calling the OpenAI API you will need to include the key in the policy configuration setup.\n\nHowever, with Flex gateway being used and openai.api.com as the upstream api we can intercept the request and dynamically inject the key from the configuration on the policy. In the example below we have assumed you have flex gateway deployed locally.\n\nNo need for the `\"Authorization: Bearer $OPENAI_API_KEY\"`\n\n```\n\u003e  curl http://localhost:8081/chat/completions \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"model\": \"gpt-3.5-turbo\",\n    \"messages\": [\n      {\n        \"role\": \"system\",\n        \"content\": \"You are an assistant, skilled in explaining MuleSoft concepts with creative flair. Keep responses free from bias and without obsenities\"\n      },\n      {\n        \"role\": \"user\",\n        \"content\": \"create a poem about Flex Gateway in less than 20 words\"\n      }\n    ]\n  }'\n  \n```\n\n## Try It\nThese setup steps use Flex Gateway in connected mode running locally on a Mac. Make sure you have docker desktop installed.\n\n### Flex Gateway\nStep 1. Open Anypoint Platform and head to Runtime Manager. Select Flex Gateways then choose Container -\u003e Docker.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"gateway-setup\" src=\"images/add-gateway.png\"\u003e\n\u003c/p\u003e\n\nStep 2. Pull the flex gateway image and start the gateway on port 8081.\n\na) Pull the latest image\n```\ndocker pull mulesoft/flex-gateway\n```\nb) Create a new directory then register the Flex Gateway to Anypoint Platform replacing `gateway-name` with your own value.\n```\ndocker run --entrypoint flexctl -u $UID \\\n  -v \"$(pwd)\":/registration mulesoft/flex-gateway \\\n  registration create --organization=adfa825c-fc0d-4ba3-a5ba-ab35a7194a39 \\\n  --token=8eb976e5-9ab1-4048-bdd6-89504008632a \\\n  --output-directory=/registration \\\n  --connected=true \\\n  \u003cgateway-name\u003e\n```\nc) Start the gateway\n```\ndocker run --rm \\\n  -v \"$(pwd)\":/usr/local/share/mulesoft/flex-gateway/conf.d \\\n  -p 8081:8081 \\\n  mulesoft/flex-gateway\n```\n\nStep 3. Head to API Manager in Anypoint. Select `Add API -\u003e Add new API`.\n\nStep 4. Make sure Flex Gateway is selected as the runtime then select the Flex Gateway you created in Step 2. Click Next.\n\nStep 5. Select Create new API. Choose `OpenAI API` as the name and and `open-ai-api` as the asset id. Click Next.\n\nStep 6. Select Port `8081` and change the base path to `/flex-api`. Click Next.\n\nStep 7. Enter `https://openai.api.com/v1` as the upstream URL\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"policy-config\" src=\"images/api-settings.png\"\u003e\n\u003c/p\u003e\n\nTest the configuration by calling the Flex Gateway endpoint to see that you get a response from OpenAI. Note that since we don't include the OpenAI API key we will get a 401 unauthorised at this stage. Note that you will need to restrict the response payload so use terms like 'in less than 20 words' to get OpenAI to return a consumable response.\n\n```\ncurl -X POST http://localhost:8081/flex-api/chat/completions   -H \"Content-Type: application/json\"  -d '{\n    \"model\": \"gpt-3.5-turbo\",\n    \"messages\": [\n      {\n        \"role\": \"system\",\n        \"content\": \"You are an assistant, skilled in explaining MuleSoft concepts with creative flair. Keep responses free from bias and without obsenities.\"\n      },\n      {\n        \"role\": \"user\",\n        \"content\": \"Compose a poem in less than 10 words.\"\n      }\n    ]\n  }'\n```\nStep 8. After downloading the policy use `make build` then `make release`.\n\nStep 9. Apply the policy in API Manager to the API created in Step 3. In the openai-api-key section enter a valid key from OpenAI.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"policy-config\" src=\"images/policy-config.png\"\u003e\n\u003c/p\u003e\n\nStep 10. Verify that the policy now successfully gets a response.\n\n## Make command reference\nThis project has a Makefile that includes different goals that assist the developer during the policy development lifecycle.\n\n*For more information about the Makefile, see [Makefile](https://docs.mulesoft.com/pdk/latest/policies-pdk-create-project#makefile).*\n\n### Setup\nThe `make setup` goal installs the Policy Development Kit internal dependencies for the rest of the Makefile goals.\nSince these dependencies are provided by the Anypoint Platform, it requires the user to be authenticated with a set of valid Anypoint credentials.\n\n*For more information about `make setup`, see [Setup the PDK Build environment](https://docs.mulesoft.com/pdk/latest/policies-pdk-create-project#setup-the-pdk-build-environment).*\n\n### Build asset files\nThe `make build-asset-files` goal generates all the policy asset files required to build, execute, and publish the policy. This command also updates the `config.rs` source code file with the latest configurations defined in the policy definition.\n\n*For more information about creating a policy definition, see [Defining a Policy Schema Definition](https://docs.mulesoft.com/pdk/latest/policies-pdk-create-schema-definition).*\n\n*For more information about `make build-asset-files`, see [Compiling Custom Policies](https://docs.mulesoft.com/pdk/latest/policies-pdk-compile-policies).*\n\n### Build\nThe `make build` goal compiles the WebAssembly binary of the policy.\nSince the source code must be in sync with the policy definition configurations, this goal runs the `build-asset-files` before compiling.\n\n*For more information about `make build`, see [Compiling Custom Policies](https://docs.mulesoft.com/pdk/latest/policies-pdk-compile-policies).*\n\n### Run\nThe `make run` goal provides a simple way to execute the current build of the policy in a Docker containerized environment. In order to run this goal, the `playground/config` directory must contain a set of files required for executing the policy in a Flex Gateway instance:\n- A `registration.yaml` file generated by performing a Flex Gateway registration in Local Mode. If you already have an instance registered in Local mode, you can reuse the registration file you have and copy it in the `playground/config` folder.\nOtherwise, to complete the registration we recommend using the Anypoint Platform:\n    1. Go to `Runtime Manager`\n    2. Navigate to the `Flex Gateway` tab\n    3. Click the `Add Gateway` button\n    4. Select `Docker` as your OS and copy the registration command replacing `--connected=true` to `--connected=false`.\n    5. Paste the command and run it in the `playground/config` directory.\n\n- An `api.yaml` file updated with the desired policy configuration. This file also supports adding other policies to be applied along the one being developed.\n\nThe `playground/config` directory can also contain other resource definitions, such as accessory services used by the policy (Eg. a remote authentication service).\n\n*For more information about `make run`, see [Debugging Custom Policies Locally with PDK](https://docs.mulesoft.com/pdk/latest/policies-pdk-debug-local).*\n\n### Test\nThe `make test` goal runs unit tests and integration tests. Integration tests are placed in the `tests` directory and are configured with the files placed at the\n`tests/\u003cmodule-name\u003e/\u003ctest-name\u003e` directory.\n\n*For more information about writing integration tests, see [Writing Integration Tests](https://docs.mulesoft.com/pdk/latest/policies-pdk-integration-tests).*\n\n### Publish\nThe `make publish` goal publishes the policy asset in Anypoint Exchange, in your configured Organization.\n\nSince the publish goal is intended to publish a policy asset in development, the _assetId_ and name published will explicitly say `dev`, and the versions published will include a timestamp at the end of the version. Eg.\n- groupId: your configured organization id\n- visible name: _{Your policy name} Dev_\n- assetId: _{your-policy-asset-id}-dev_\n- version: _{your-policy-version}-20230618115723_\n\n*For more information about publishing policies, see [Uploading Custom Policies to Exchange](https://docs.mulesoft.com/pdk/latest/policies-pdk-publish-policies).*\n\n### Release\nThe `make release` goal also publishes the policy to Anypoint Exchange, but as a ready for production asset. In this case, the groupId, visible name, assetId and version will be the ones defined in the project.\n\n*For more information about releasing policies, see [Uploading Custom Policies to Exchange](https://docs.mulesoft.com/pdk/latest/policies-pdk-publish-policies).*\n\n\n### Policy Examples\n\nThe PDK provides provides a set of example policy projects to get started creating policies and using the PDK features. To learn more about these examples see [Custom policy Examples](https://docs.mulesoft.com/pdk/latest/policies-pdk-policy-templates).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeejay-hub%2Fopenai-api-key-mgmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeejay-hub%2Fopenai-api-key-mgmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeejay-hub%2Fopenai-api-key-mgmt/lists"}