{"id":15103483,"url":"https://github.com/ronnyhaase/angular-apikeys","last_synced_at":"2025-09-27T01:31:02.964Z","repository":{"id":57178002,"uuid":"49726787","full_name":"ronnyhaase/angular-apikeys","owner":"ronnyhaase","description":"A Angular module with a service for a more elegant sharing and storing of your API keys","archived":true,"fork":false,"pushed_at":"2016-01-29T09:17:06.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T23:03:11.288Z","etag":null,"topics":["angular","angularjs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/ronnyhaase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-15T15:22:10.000Z","updated_at":"2023-01-28T04:22:08.000Z","dependencies_parsed_at":"2022-09-14T02:31:10.198Z","dependency_job_id":null,"html_url":"https://github.com/ronnyhaase/angular-apikeys","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ronnyhaase%2Fangular-apikeys","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ronnyhaase%2Fangular-apikeys/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ronnyhaase%2Fangular-apikeys/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ronnyhaase%2Fangular-apikeys/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ronnyhaase","download_url":"https://codeload.github.com/ronnyhaase/angular-apikeys/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871823,"owners_count":16554457,"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":["angular","angularjs"],"created_at":"2024-09-25T19:24:21.289Z","updated_at":"2025-09-27T01:30:57.655Z","avatar_url":"https://github.com/ronnyhaase.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# angular-apikeys\nA Angular module with a service for a more elegant sharing and storing of your API keys\n\nCopyright (c) Ronny Haase, 2016.\n\nLicensed under The MIT License.\n\nhttps://github.com/ronnyhaase/angular-apikeys\n\n## Introduction\nIf you ever run into the problem of talking to a lot of APIs in your AngularJS application and needed a place to manage the API keys, this tiny, unfancy module with just one lonesome service **(backed by unit tests)** is for you!\n\nIt's also used as optional dependency by other modules and services implementing APIs, as a kind of registry. *(More details [here](#using-angular-apikeys-as-registry))*\n\n## Why\nBecause it\n\n1. Decouples the problem of holding API keys\n3. Can be used as a generic way of handling API keys across projects\n2. Is more elegant than solving this with `angular.constant` or `angular.value` especially across multiple modules\n\n## Installation\n\nUse `npm`:\n\n\t$ npm install angular-apikeys\n\nOr `Bower`:\n\n\t$ bower install angular-apikeys\n\nOr grab the latest [release](https://github.com/ronnyhaase/angular-apikeys/releases) and add the JS file manually.\n\n```html\n\u003cscript src=\"angular-apikeys.js\"\u003e\u003c/script\u003e\n```\nOr minified:\n\n```html\n\u003cscript src=\"angular-apikeys.min.js\"\u003e\u003c/script\u003e\n```\n\n## HowTo\n\nThe module is *\"apikeys\"* and it only consists of the service *\"apiKeys\"*, which has the methods `get(id)`, `set(id, apikey)` and `has(id)`.\n\n```javascript\nangular.module('myApp', ['apikeys']);\n\nangular.module('myApp').run(['apiKeys', function (apiKeys) {\n\tfetchMyApiKeysSomehow().then(function(keys) {\n\t\tapiKeys.set('google.maps.geocode', keys.google.geocode);\n\t});\n}]);\n\nangular.module('myApp').factory('googleGeocode', ['apiKeys', function (apiKeys) {\n\t// ...\n\t$http({\n\t\turl: 'https://maps.googleapis.com/maps/api/geocode/json',\n\t\tparams: {\n\t\t\tkey: apiKey.has('google.maps.geocode')\n\t\t\t\t? apiKeys.get('google.maps.geocode')\n\t\t\t\t: '12345'\n\t\t}\n\t\t// ...\n\t});\n\t// ...\n}]);\n```\n\nAs I said: Its unfancy.\n\nBoth, key and value must be non-empty strings.\n\nAngular-apikeys is unoptionated about how you name your keys, but the lower case dot notation, seems to make sense to me.\n\nAlso notice, that it's currently possible to overwrite existing keys with `set()`. But for now it didn't seem to make sense to add more logic around such problems.\n\nThat's it, the rest is up to you.\n\n---\n\nAlso, since ApiKeys is a provider you can use it in the configuration phase of AngularJS, e.g. to do crazy things with $httpProvider:\n\n```javascript\nangular.module('myOtherApp').config('apiKeysProvider', [function (apiKeys) {\n\t// Same API\n\tapiKeys.set('googlePlaces', '0987654321');\n\tapiKeys.get('googlePlaces');\n}]).run(function (apiKeys) {\n\t// Already available for a long time...\n\tapiKeys.get('googlePlaces');\n});\n```\n\n---\n\nThings like read-only, more strict key/value validation or other crazy ideas may be added later, if necessary.\n\n## API Reference\n\n### Module: apikeys\n\n#### Dependencies: *none*\n\n#### apiKeys\nService (provider) in module apikeys\n\n##### Description\nStores and delivers API keys.\n\n##### Methods\n\n###### get(id)\nReturns a API key by it's ID.\n\n**Arguments**\n\n| Param | Type     | Details                       |\n|-------|----------|-------------------------------|\n| id    | `String` | The ID of the key to fetch    |\n\n**Returns**\n\n`String | undefined | false` The key of the requested ID, or `undefined` if it's unknown, or `false` in case of an error.\n\n---\n\n###### has(id)\nReturns if a API key ID is defined.\n\n**Arguments**\n\n| Param | Type     | Details                       |\n|-------|----------|-------------------------------|\n| id    | `String` | The ID of the key to look up  |\n\n**Returns**\n\n`Boolean` If the API key is defined\n\n---\n\n###### set(id, apikey)\nSets or resets a API key.\n\n**Arguments**\n\n| Param  | Type     | Details                  |\n|--------|----------|--------------------------|\n| id     | `String` | The ID of the key to set |\n| apikey | `String` | The API key to store     |\n\n**Returns**\n\n`Boolean` If the API key was successfully stored\n\n## Using angular-apikeys as registry\n\nLet's see this with an example.\n\nImagine you and your team build an application around Google Maps APIs which provide quiet a lot APIs, and you split modules and services - what totally makes sense.\n\nLikely you want to reuse your services for Google Maps APIs across projects.\n\nBy injecting angular-apikeys without declaring it explicitly as dependency, your services can provide a generic way to provide them with API keys, but neither your API modules \u0026 services nor other applications/modules usign them need to use angular-apikeys.\n\nThis is how it could look like:\n\n[See on Plunker](http://plnkr.co/edit/eE98GLKgDFAqItji1bPR?p=preview)\n\n````javascript\n// (No \"hard\" dependency to apiKeys)\nangular.module('google.maps', []);\n\nangular.module('google.maps')\n  .provider('geocoding', function () {\n    var ID_APIKEY = 'google.maps.geocoding'; // A Unique ID for our API key\n\n    var apiKey = null;\n\n    // For folks not using angular-apikeys\n    this.setApiKey = function (key) {\n      if (angular.isString(key)) {\n        apiKey = key;\n      }\n\n      return !!apiKey;\n    };\n\n    this.$get = ['$injector', function ($injector) {\n      // Check via $injector.has, $injector.get throws if service is unknown!\n      var apiKeys = $injector.has('apiKeys')\n          ? $injector.get('apiKeys')\n          : null;\n\n      // Prefer apiKeys (you don't have to, of course)\n      if (apiKeys \u0026\u0026 apiKeys.has(ID_APIKEY)) {\n        apiKey = apiKeys.get(ID_APIKEY);\n      }\n\n      // Can't live without\n      if (!apiKey) {\n        throw 'You must provide a API key via apiKeys or manually with setApiKey() via the provider'\n        return null;\n      }\n\n      console.log('google.maps.geocoding is all set up!');\n\n      // ... Your super awesome service code ...\n\n      return {\n        // ...\n        _apiKey: apiKey\n      };\n    }];\n  })\n  .provider('geolocation', function () {\n    var ID_APIKEY = 'google.maps.geolocation';\n\n    // All the same as geocoding-service\n    // ...\n    this.$get = {};\n  });\n\n\n\nangular.module('myApp', ['apikeys', 'google.maps']);\n\n// Now we set the expected ID...\nangular.module('myApp').config(['apiKeysProvider', function (apiKeys) {\n  apiKeys.set('google.maps.geocoding', '1234567890');\n  apiKeys.set('google.maps.geolocation', '1234567891');\n  // ...\n}]);\n\n\n// ... and geocoding automagically has it's API key\nangular.module('myApp').run(['geocoding', function (geocoding) {\n  console.log(geocoding._apiKey === '1234567890'); // true\n}]);\n````\n\n## Contributing\nPlease create an issue. If you add a pull request, try to respect my code style, check for JSHint and assure the unit tests do pass, and extend them if necessary!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronnyhaase%2Fangular-apikeys","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fronnyhaase%2Fangular-apikeys","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronnyhaase%2Fangular-apikeys/lists"}