While we recommend using webhooks to get updates into Nacelle, there may be instances where an automated re-index may be necessary. One such example is with updates to product variants, which aren't yet captured by webhooks. In these instances you can use the Admin API to automate the re-index process.
Setting Up the Admin API
To start you'll need to have a Nacelle representative activate the Admin API. This can be accomplished by reaching out to your account manager, or creating a support ticket.
Once the Admin API is active you'll notice a new section in the Nacelle dashboard, under Settings > API Details. Use this section to create a new token. Save this token's value, we'll be using it in the next step.
Creating the Mutation
Next we'll create a GraphQL mutation. This will reach out to an endpoint, and let Nacelle know we'd like to start a re-index. For this step, I will be using Insomnia to create, and test my mutation. We'll then take this mutation, and convert it to JS later.
Start by setting the endpoint:
https://admin.api.nacelle.com/graphql
Next, add your headers:
Content-Type: application/json
x-nacelle-space-id: your-space-id
x-nacelle-admin-token: your-admin-token
Finally, we can add our mutation:
mutation startReIndex {
startIndex(sourceId: "your-source-id") {
indexId
success
failureReason
}
}
You'll need to get your Source ID from the Nacelle dashboard. It can be found under Settings > Data Sources. Each data source has it's own unique ID.
Alternatively, you can also choose to re-index using a different metric, such as content type, or entry ID. See the docs for all options.
Here's what our final mutation looks like:
To test this mutations, send the request, and then look in the Data Sources section of the dashboard. You should see that a re-index has started.
Converting to JS
Finally, once we're certain this request works, we can turn it into JS:
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-nacelle-space-id': 'your-space-id',
'x-nacelle-admin-token': 'your-admin-token'
},
body: '{"query":"mutation startReIndex {\n\tstartIndex(sourceId: \"your-source-id\") {\n\t\tindexId\n\t\tsuccess\n\t\tfailureReason\n\t}\n}","operationName":"startReIndex"}'
};
fetch('https://admin.api.nacelle.com/graphql', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Comments
0 comments
Please sign in to leave a comment.