Introduction
Grammarly helps improve text quality in modern web applications. In 2026, its API integration provides precise control over real-time grammar and style corrections. This tutorial guides you step by step through implementing it in React by combining user interface components with server-side logic.
Prerequisites
- Node.js 20+ and React 18
- Grammarly Business account with API key
- Solid knowledge of TypeScript and Next.js
Install Dependencies
npm install @grammarly/editor-sdk-react axios
npm install -D @types/nodeThis command installs the official Grammarly React SDK and Axios for API calls. Avoid outdated versions that cause conflicts with React hooks.
Configure the .env File
NEXT_PUBLIC_GRAMMARLY_CLIENT_ID=votre_client_id
GRAMMARLY_API_KEY=votre_cle_api_secreteStore your Grammarly credentials in environment variables. Never expose them on the client side for security reasons.
React Component with Grammarly
import React from 'react';
import { GrammarlyEditorPlugin } from '@grammarly/editor-sdk-react';
export const GrammarlyEditor: React.FC = () => {
return (
<div style={{ height: '400px' }}>
<textarea placeholder="Rédigez votre texte ici..." />
<GrammarlyEditorPlugin clientId={process.env.NEXT_PUBLIC_GRAMMARLY_CLIENT_ID!} />
</div>
);
};This component integrates the Grammarly plugin directly into a textarea. The clientId enables real-time suggestions without page reloads.
API Route for Advanced Checking
import { NextResponse } from 'next/server';
import axios from 'axios';
export async function POST(request: Request) {
const { text } = await request.json();
const response = await axios.post('https://api.grammarly.com/v1/check', { text }, {
headers: { Authorization: `Bearer ${process.env.GRAMMARLY_API_KEY}` }
});
return NextResponse.json(response.data);
}This secure server route sends text to the Grammarly API and returns corrections. Handle 429 errors for rate limiting.
Custom Hook for API Calls
import { useState } from 'react';
export function useGrammarlyCheck() {
const [corrections, setCorrections] = useState([]);
const checkText = async (text: string) => {
const res = await fetch('/api/grammarly', { method: 'POST', body: JSON.stringify({ text }) });
const data = await res.json();
setCorrections(data.suggestions);
};
return { corrections, checkText };
}This React hook encapsulates the API call logic. It enables easy reuse across multiple components while maintaining suggestion state.
Best Practices
- Always validate user input before sending to the API
- Use Grammarly's offline mode for sensitive text
- Limit API calls to 1 per second
- Cache results for 30 seconds
- Test with multilingual text
Common Mistakes to Avoid
- Forgetting to hide the API key on the client side
- Ignoring API rate limits
- Not handling network errors in hooks
- Using incompatible versions of the React SDK
Going Further
Explore advanced Grammarly API features and discover our training programs to master complex integrations.