Don’t have your Algolia credentials yet? Apply to DocSearch to get free access for your open-source documentation.
What You’ll Need
Before you begin, you’ll need:- An Algolia Application ID (
appId) - An Algolia Search-Only API Key (
apiKey) - Your DocSearch Index Name (
indexName)
Choose Your Implementation
DocSearch is available as both a vanilla JavaScript package and a React component. Choose the implementation that matches your documentation framework:JavaScript
For vanilla JavaScript, HTML sites, or any framework
React
For React, Next.js, Gatsby, or React-based frameworks
JavaScript Implementation
Installation
Install the JavaScript package using your preferred package manager:Add a Container
Add a container element to your HTML where DocSearch will render:index.html
Initialize DocSearch
Import and initialize DocSearch with your credentials:main.js
Programmatic Control
Thedocsearch() function returns an instance with methods for programmatic control:
Complete Example
Here’s a complete working example:React Implementation
Installation
Install the React package using your preferred package manager:Basic Usage
Import theDocSearch component and render it in your application:
App.jsx
TypeScript Support
DocSearch has full TypeScript support with type definitions included:App.tsx
Using a Ref
Access the DocSearch instance programmatically using a ref:Complete Example
Here’s a complete React example with customization:Verify It’s Working
After implementing DocSearch, you should see:Search Button Appears
A search button with the text “Search” (or your custom text) and keyboard shortcut hint (Ctrl+K or ⌘K) appears in your application.
Modal Opens on Click
Clicking the search button or pressing Ctrl+K (⌘K on Mac) opens the search modal.
If you’re not seeing results, verify that:
- Your credentials are correct
- Your index has been crawled and contains documents
- Your API key has search permissions for the index
Import Styles
Both implementations require importing the CSS styles. DocSearch provides styles as a separate package:- Search button styling
- Modal and overlay styling
- Result list styling
- Responsive design for mobile devices
- Dark mode support (automatic based on system preferences)
Keyboard Shortcuts
DocSearch comes with built-in keyboard shortcuts:| Shortcut | Action |
|---|---|
Ctrl+K / ⌘K | Open search modal |
/ | Open search modal |
Esc | Close search modal |
↑ / ↓ | Navigate results |
Enter | Go to selected result |
Tab | Navigate to next section |
Shift+Tab | Navigate to previous section |
Next Steps
Now that you have DocSearch working, explore these features:Styling
Customize colors, fonts, and layout to match your brand
API Reference
Explore all configuration options and methods
Advanced Features
Learn about transformItems, custom components, and more
Ask AI
Enable AI-powered conversational search
Troubleshooting
Search button not appearing
Make sure you’ve:- Imported the CSS:
import '@docsearch/css'; - Provided a valid container selector
- Called
docsearch()after the DOM is ready
No search results
Check that:- Your Algolia credentials are correct
- The index name matches your DocSearch index
- Your index has been crawled and populated
- Your API key has search permissions
Styles not loading
Ensure:- You’ve imported
@docsearch/cssin your entry file - Your bundler is configured to handle CSS imports
- The CSS is loaded before rendering the component
TypeScript errors
DocSearch includes TypeScript definitions. Make sure:- You’re using
@docsearch/react@4or@docsearch/js@4 - Your
tsconfig.jsonincludes the node_modules directory - You’re importing types correctly:
import type { DocSearchProps } from '@docsearch/react';
Get Help
If you need assistance:- Check the API Reference for detailed documentation
- Browse examples on GitHub
- Visit the DocSearch website
- Join the Algolia Community
