0

I am new to Drupal module development, and have searched but haven't found any tutorials or guides on how to create a module to do the following:

  1. Access a 3rd party API via a form
  2. Store the user API key in the database
  3. Access the API key when the user uses the 3rd party API via a form submission

Can someone show me how or point me in the right direction for at least steps 2 and 3?

1

You can handle the storage and retrieval of the API keys in the database via the Drupal API functions: variable_set and variable_get.

To store the key (step 2):

variable_set('mymodule_api_key', $api_key);

To access the key (step 3):

$api_key = variable_get('mymodule_api_key', t('-key not set-'));

Where "mymodule" is the short name of your custom module.

As for a comprehensive example: There are many bridge modules in the Drupal.org repos that connects to an external service using an API and authenticates using API keys. One example of this is Mollom.

3
  • Cheers for this! Are there any examples on how to create a module to access 3rd party APIs in Drupal? I've searched but haven't found any clear examples to learn from. – shepherd Mar 18 '15 at 1:38
  • I've updated the answer. – Free Radical Mar 18 '15 at 1:52
  • Thanks, but there isn't any guides to show you how to build one from scratch? For example, I saw this: drupal.org/node/1228830 – shepherd Mar 18 '15 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.