How to Hide Your API Keys Safely in Flutter (The Right Way in 2025)
Teqani Blogs
Writer at Teqani
API keys are like passwords — they grant access to services that power your app. Exposing them publicly (especially in version control or app binaries) can lead to abuse, security breaches, and unexpected costs. This article provides two clean and safe ways to hide your API keys in a Flutter project. It also covers handling multiple keys, debugging missing configurations, and automating key injection into your app builds.
تُشبه مفاتيح واجهة برمجة التطبيقات (API) كلمات المرور - فهي تمنح الوصول إلى الخدمات التي تدعم تطبيقك. يمكن أن يؤدي الكشف عنها علنًا (خاصةً في التحكم في الإصدار أو الملفات الثنائية للتطبيق) إلى إساءة الاستخدام والاختراقات الأمنية والتكاليف غير المتوقعة. تقدم هذه المقالة طريقتين نظيفتين وآمنتين لإخفاء مفاتيح واجهة برمجة التطبيقات الخاصة بك في مشروع فلاتر. كما يغطي التعامل مع مفاتيح متعددة وتصحيح أخطاء التكوينات المفقودة وأتمتة حقن المفاتيح في إصدارات تطبيقك.
Why You Should Never Hardcode API Keys
Before jumping in, here’s a quick reminder why hiding your keys matters:
قبل البدء، إليك تذكير سريع بأهمية إخفاء مفاتيحك:
- They can leak via GitHub, public repos, or screenshots.
- Once reverse engineered, your production keys can be abused, leading to account suspension or bills.
- Even if obfuscated, hardcoded keys can be retrieved via decompiling APKs.
- يمكن أن تتسرب عبر GitHub أو المستودعات العامة أو لقطات الشاشة.
- بمجرد الهندسة العكسية، يمكن إساءة استخدام مفاتيح الإنتاج الخاصة بك، مما يؤدي إلى تعليق الحساب أو الفواتير.
- حتى إذا تم إخفاؤها، يمكن استرداد المفاتيح المبرمجة عن طريق تجميع ملفات APK.
Method 1: Using --dart-define (for Build-Time Injection)
Inject environment variables using the --dart-define flag when running your app:
حقن متغيرات البيئة باستخدام علامة --dart-define عند تشغيل تطبيقك:
flutter run --dart-define=API_KEY=your-secret-key
Inside your Dart code:
داخل كود Dart الخاص بك:
const apiKey = String.fromEnvironment('API_KEY');
if (apiKey.isEmpty) {
throw Exception('API_KEY is missing!');
}
--dart-define-from-file for Multiple Keys
Create keys.json:
إنشاء keys.json:
{
"API_KEY": "your-secret-api-key",
"DB_KEY": "your-db-secret"
}
Run with File:
تشغيل مع ملف:
flutter run --dart-define-from-file=keys.json
VS Code Setup for Ctrl+F5 Users
If you’re using Ctrl + F5 in VS Code and the keys don’t load:
إذا كنت تستخدم Ctrl + F5 في VS Code والمفاتيح لا يتم تحميلها:
- Open the Run and Debug tab.
- Click the gear icon (⚙️) to edit launch.json.
- Under your config, add:
"args": ["--dart-define-from-file=keys.json"]
- افتح علامة التبويب تشغيل وتصحيح الأخطاء.
- انقر فوق رمز الترس (⚙️) لتحرير launch.json.
- ضمن التكوين الخاص بك ، أضف:
"args": ["--dart-define-from-file=keys.json"]
Method 2: Using .env File with envied Package
Prefer working with .env files like in Node.js or Python? Flutter supports that too.
هل تفضل العمل مع ملفات .env كما هو الحال في Node.js أو Python؟ يدعم فلاتر ذلك أيضًا.
- Add Dependencies in your pubspec.yaml:
- أضف التبعيات في pubspec.yaml الخاص بك:
dependencies:
envied: ^0.3.0
dev_dependencies:
envied_generator: ^0.3.0
build_runner: ^2.4.0
- Create .env File:
- إنشاء ملف .env:
API_KEY=your-secret-key
DB_KEY=your-db-key
- Create env.dart:
- إنشاء env.dart:
import 'package:envied/envied.dart';
part 'env.g.dart';
@Envied(path: '.env', obfuscate: true)
abstract class Env {
@EnviedField(varName: 'API_KEY')
static const String apiKey = _Env.apiKey;
@EnviedField(varName: 'DB_KEY')
static const String dbKey = _Env.dbKey;
}
- Generate Code:
- توليد التعليمات البرمجية:
flutter pub run build_runner build
Why use obfuscate: true
? Without it, your keys are exposed in the generated file. With obfuscation enabled, your secrets are encoded and harder to reverse-engineer.
لماذا تستخدم obfuscate: true
؟ بدون ذلك ، يتم الكشف عن مفاتيحك في الملف الذي تم إنشاؤه. مع تمكين التعتيم ، يتم ترميز أسرارك ويصعب عكس هندستها.
Final Thoughts
Securing your API keys is not just a good practice — it’s a necessary step for any production app. Whether you prefer --dart-define
for simplicity or envied
for structured environments, both methods offer strong protection when done right.
إن تأمين مفاتيح واجهة برمجة التطبيقات الخاصة بك ليس مجرد ممارسة جيدة - بل هو خطوة ضرورية لأي تطبيق إنتاج. سواء كنت تفضل --dart-define
للبساطة أو envied
للبيئات المنظمة ، فإن كلتا الطريقتين توفران حماية قوية عند القيام بهما بشكل صحيح.
Never push your keys.json
or .env
to GitHub. Add them to .gitignore
. Use CI/CD pipelines to inject secrets during deployment instead of committing them.
لا تدفع أبدًا keys.json
أو .env
إلى GitHub. أضفهم إلى .gitignore
. استخدم خطوط أنابيب CI/CD لحقن الأسرار أثناء النشر بدلاً من الالتزام بها.
All blogs are certified by our company and reviewed by our specialists
Issue Number: #c426918e-da62-44cb-8f67-7c2df8a48235