Introducing the Acquia DAM SDK for Javascript and Typescript
- Last updated
- minute read
Goal
Learn about the Acquia DAM SDK and how to apply it to real-world applications.
How to use
-
Installation
To install the Acquia DAM SDK in a Javascript or Typescript project, use your preferred package manager's syntax.
NPM:
npm install acquia-dam-sdk
Yarn:
yarn add acquia-dam-sdk
-
Import the package into a file
To use the SDK in a file, you must import the package. Both ESModule and CommonJs syntaxes are supported.
// ESModule Syntax import AcquiaDAM from 'acquia-dam-sdk' // CommonJs Syntax const AcquiaDAM = require('acquia-dam-sdk').default
-
Create an instance of the AcquiaDAM class
Create an instance of the AcquiaDAM class using your access token
See our API FAQ Support article for more information on access tokens
const dam = new AcquiaDAM({ accessToken: 'YOUR TOKEN HERE' })
-
Develop!
From here, it's fully up to you! By using the provided documentation on docs.acquia.com, you'll be able to build any sort of tool for your Acquia DAM. The possibilities are endless!
Features
- Fully Typed Interface: The Acquia DAM SDK provides types for all requests and responses, as well as parameter descriptions. Your code editor should automatically pick up on the documentation and generate tooltips and autocomplete suggestions as you code. This includes types for incoming Webhook events.
- Modular: Functions are grouped into logical components, making it easy to use what you need and ignore what you don’t.
- Modern Javascript Syntax: Utilize modern Javascript syntax for clean, readable code. E.g.,
// Use Destructuring syntax to obtain references to multiple components const { assets, searchConnector } = new AcquiaDAM({ accessToken: 'YOUR TOKEN HERE' })
- Flexible and Extendable: Use the ApiClient directly to create custom API calls, or extend the ApiClient to implement automatic retries, error handling, or handling of OAuth token exchanges.
Can I use this SDK in a front-end application?
“Yes, but…”
The authentication options supported by the Acquia DAM API (Personal Access Token and OAuth Authorization Code) are intended to be used by servers making requests on behalf of the user, not by the user themselves in a browser environment. For security reasons, we do not recommend using the SDK in the browser as-is.
However, a proxy server can help with using the SDK on the front-end. Instead of having users send requests directly from their browser to the Acquia DAM API, they will send requests to your proxy server. The browser (providing the appropriate browser-based authentication, such as a session cookie or JWT) will send the request to the proxy, and the proxy will apply the correct authentication for DAM and make the request on behalf of the user. To achieve this with the SDK we provide, you will need to extend the ApiClient class with your own implementation of the buildHeaders, buildUrl, and sendRequest functions. Then, you will pass an instance of your client to the AcquiaDAM class.
Example code (some setup required)
// MyProxyClient.ts
import { AcquiaDAMError, ApiClient } from 'acquia-dam-sdk/client'
import type { ApiRequestParams } from 'acquia-dam-sdk/client/types'
export class MyProxyClient extends ApiClient {
// Add a custom constructor for your class, if necessary
/**
* Custom implementation of the buildUrl function
* @param requestParams Information about the request
* @returns The full URL to send
*/
protected buildUrl(requestParams: ApiRequestParams) {
const base = 'https://MYPROXYSERVER/' // Set the base URL to your proxy server. Ensure to include the trailing '/' character
const queryString = requestParams.queryStringParams
? `?${new URLSearchParams(
this.buildParams(requestParams.queryStringParams)
)}`
: ''
return `${base}${requestParams.path}${queryString}`
}
/**
* Custom implementation of the buildHeaders function
* @param requestParams Information about the request
* @returns The necessary headers to call the Proxy server
*/
protected buildHeaders(requestParams: ApiRequestParams) {
const headers = new Headers()
headers.set('x-acquia-sdk-client', this.__sdkIdentifier)
// Set any additional headers required for your proxy server
if (requestParams.body && !(requestParams.body instanceof FormData)) {
headers.set('Content-Type', 'application/json')
}
return headers
}
/**
* Custom implementation of the sendRequest method
* @param requestParams Information about the request to send
* @returns The data in the response body
*/
public async sendRequest<T>(requestParams: ApiRequestParams): Promise<T> {
const opts: RequestInit = {
method: requestParams.method,
headers: this.buildHeaders(requestParams),
}
const url = this.buildUrl(requestParams)
if (requestParams.body) {
opts.body = this.buildBody(requestParams)
}
// Implement any additional logic needed to call your proxy server
const response = await fetch(url, opts)
if (!response.ok) {
const content = await this.parseBody<string | object | undefined>(
response
)
throw new AcquiaDAMError('HTTP Error', response.status, content)
}
return this.parseBody(response)
}
}
// MyProgram.ts
import AcquiaDAM from 'acquia-dam-sdk'
import { MyProxyClient } from './MyProxyClient'
const myClient = new MyProxyClient()
const dam = new AcquiaDAM({ client: myClient })
Contributing
As an open-source project we encourage community collaboration for improvements. Visit the project’s Github repository for more information.