1

I was able to do this using the gcloud CLI:

gcloud --project=some-project alpha services api-keys create

But I could not find any way to do this using googleapis, nor was I able find any leads at their node repository google-api-nodejs-client.

For context, I will be running this functions in AWS Lambda.

2 Answers 2

1

I think (!?) that this API is not yet exposed through APIs Explorer:

E.g. The following 404s (NOT_FOUND)

API=apkeys
VER=v2alpha1

curl https://www.googleapis.com/discovery/v1/apis/${API}/${VER}/rest

Unfortunately, until it is, (there's no discovery document and) the API Client library is unable to auto-generate the SDK for it.

It's unclear to me whether this is policy or an oversight.

I recommend you pester the Cloud SDK team on Google's Issue Tracker (for Cloud SDK)

Note:

If you append --log-http to (any) gcloud command, it will display the underlying REST calls for the command. Absent a Google-provided SDK for these methods, you could introspect the API and code the REST calls directly:

gcloud alpha services api-keys create ... \
--project=${PROJECT} \
--log-http

Yields:

==== request start ====
uri: https://apikeys.googleapis.com/v2alpha1/projects/${PROJECT}/keys?alt=json
method: POST
== headers start ==
b'accept': b'application/json'
b'authorization': b'Bearer ya29...'
== headers end ==
== body start ==
== body end ==
Sign up to request clarification or add additional context in comments.

Comments

0

New: It is now possible to create API Key using @google-cloud/apikeys package

For anybody wondering this recently.

This is the example provided from official Github:

https://github.com/googleapis/google-cloud-node/blob/main/packages/google-api-apikeys/samples/generated/v2/api_keys.create_key.js

// Imports the Apikeys library
const {ApiKeysClient} = require('@google-cloud/apikeys').v2;

// Instantiates a client
const apikeysClient = new ApiKeysClient();

async function callCreateKey() {
  // Construct request
  const request = {
    parent,
    key,
  };

  // Run request
  const [operation] = await apikeysClient.createKey(request);
  const [response] = await operation.promise();
  console.log(response);
}

callCreateKey();

Check out the related document as well https://cloud.google.com/nodejs/docs/reference/apikeys/latest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.