Getting Started with Magento REST API and Postman
As a beginner exploring the Magento REST API, you’ll need to authenticate your requests using OAuth. Here’s a step-by-step guide on how to obtain the necessary token and token secret for making authenticated requests in Postman.
Understanding Magento REST API Authentication
Magento uses OAuth 1.0a for all its REST API interactions. This means we need to generate tokens (consumer key/secret) and access tokens (token/token secret).
What you already have:
- Consumer Key
- Consumer Secret
What we’ll obtain in this guide:
- Token
- Token Secret
1. Generate an Access Token
First, navigate to your Magento admin panel. Go to System > Web Services > OAuth Tokens. Click on the “Add New Token” button.
Fill in the following fields:
- Name: Enter a descriptive name for easy identification.
- Status: Set it to Enabled.
- Roles: Choose the role that the token should have (e.g., Admin).
- Website: Select the website where this token will be valid.
- Customer: Leave as None.
- Role User: Leave as None.
Click Save Token.
2. Retrieve the Token and Token Secret
After saving, you’ll see a screen displaying the new token’s details. Make note of:
Token: This is your access token/name. Token Secret: This is your token secret.
Keep these values safe; they are sensitive information required for authenticating requests.
3. Configuring Postman with Magento REST API
Open Postman and create a new request.
Step A: Set the Request Method
Set the method to POST. We’re sending an authentication request.
Step B: Enter the URL
Enter your Magento site URL followed by /oauth/token. For example:
https://yourmagentosite.com/oauth/token
Step C: Add Headers
Add the following headers:
- Content-Type:
application/x-www-form-urlencoded - Accept:
application/json
Step D: Set Request Body
In the request body, add these key-value pairs:
grant_type=password&username=your_username&password=your_password&consumer_key=your_consumer_key&consumer_secret=your_consumer_secret
Replace the placeholders with your Magento admin username, password, consumer key, and consumer secret.
Step E: Send the Request
Click on Send. If everything is configured correctly, you should receive a JSON response containing your access token details.
4. Accessing Protected Resources
To make authenticated requests to actual resources (like products or customers), set up your request as follows:
- Set the method to GET.
- Enter the URL of the resource you want to access (e.g.,
/rest/V1/catalog_product). - Add these headers:
- Authorization:
OAuth oauth_consumer_key="your_consumer_key",oauth_token="your_access_token",oauth_signature_method="HMAC-SHA256",oauth_version="1.0",oauth_nonce="random_string",oauth_timestamp="current_timestamp" - Accept:
application/json
- Authorization:
Replace the placeholders with your consumer key, access token (obtained in step 2), a random nonce, and the current timestamp.
Send the request to retrieve the data from the protected resource.
Conclusion
You have now successfully obtained an access token and token secret, and learned how to configure Postman for making authenticated requests to the Magento REST API using OAuth. Remember to keep your tokens secure and update them periodically for enhanced security.
For more advanced use cases like handling refresh tokens or working with multiple tokens, I recommend checking out Laravel Company’s guide on Magento authentication for a deeper dive into the subject.