<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jeremy Persing</title>
    <description>The latest articles on DEV Community by Jeremy Persing (@jeremypersing).</description>
    <link>https://dev.to/jeremypersing</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F778026%2F18bce21f-22cf-49ca-b744-d934f9d2c8a0.jpeg</url>
      <title>DEV Community: Jeremy Persing</title>
      <link>https://dev.to/jeremypersing</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeremypersing"/>
    <language>en</language>
    <item>
      <title>React Native Stocks App Tutorial</title>
      <dc:creator>Jeremy Persing</dc:creator>
      <pubDate>Fri, 26 Jan 2024 23:35:49 +0000</pubDate>
      <link>https://dev.to/jeremypersing/react-native-stocks-app-tutorial-1o4h</link>
      <guid>https://dev.to/jeremypersing/react-native-stocks-app-tutorial-1o4h</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VWqGHUKh3bo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  The Project
&lt;/h2&gt;

&lt;p&gt;This project goes through the basics of creating and styling components in React Native. You will learn how to take JSON data and turn that into something meaningful and pretty for your users.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You'll Implement/Learn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;React Native Gesture Handler&lt;/li&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;FlatLists&lt;/li&gt;
&lt;li&gt;FlexBox&lt;/li&gt;
&lt;li&gt;React Native Gifted Charts&lt;/li&gt;
&lt;li&gt;Expo Router&lt;/li&gt;
&lt;li&gt;Expo&lt;/li&gt;
&lt;li&gt;Expo Image&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and the list goes on.&lt;/p&gt;

&lt;p&gt;This project uses version 50 of Expo, so in the future things may look different.&lt;/p&gt;

&lt;p&gt;Documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React Native Paper: &lt;a href="https://callstack.github.io/react-native-paper/docs/guides/getting-started"&gt;https://callstack.github.io/react-native-paper/docs/guides/getting-started&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Native Paper Theming: &lt;a href="https://callstack.github.io/react-native-paper/docs/guides/theming"&gt;https://callstack.github.io/react-native-paper/docs/guides/theming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Gestures: &lt;a href="https://docs.expo.dev/tutorial/gestures/"&gt;https://docs.expo.dev/tutorial/gestures/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Native Gifted Charts: &lt;a href="https://gifted-charts.web.app/"&gt;https://gifted-charts.web.app/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Async Storage: &lt;a href="https://react-native-async-storage.github.io/async-storage/docs/install"&gt;https://react-native-async-storage.github.io/async-storage/docs/install&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Expo: &lt;a href="https://docs.expo.dev/get-started/installation/"&gt;https://docs.expo.dev/get-started/installation/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Expo Icons: &lt;a href="https://icons.expo.fyi/Index"&gt;https://icons.expo.fyi/Index&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Data: &lt;a href="https://revesta.net"&gt;https://revesta.net&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Stripe Payments with Nextjs</title>
      <dc:creator>Jeremy Persing</dc:creator>
      <pubDate>Thu, 02 Nov 2023 00:41:57 +0000</pubDate>
      <link>https://dev.to/jeremypersing/stripe-payments-with-nextjs-5fnl</link>
      <guid>https://dev.to/jeremypersing/stripe-payments-with-nextjs-5fnl</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/uQAf6huBIks"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Github: &lt;a href="https://github.com/JeremyPersing/stripe-next-payments/tree/main" rel="noopener noreferrer"&gt;stripe-next-payments&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Stripe is a payments processor that allows developers like me and you to easily collect payments on our websites.&lt;br&gt;
In this post I’ll show you how you can use stripe payments in your nextjs application.&lt;br&gt;
I’m going to start from the very beginning and you can pick up wherever you want based on the steps below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - Creating a Nextjs project
&lt;/h2&gt;

&lt;p&gt;If you don’t already have it installed, install nodejs and then enter in the following command&lt;br&gt;
&lt;code&gt;npx create-next-app@latest&lt;/code&gt;. I went with the following defaults.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1suzg1ln76a3wjwbarr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1suzg1ln76a3wjwbarr3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create a nextjs project on your machine and you will need to open this in your favorite code editor.&lt;br&gt;
Once you have that done, run the command &lt;code&gt;npm run dev&lt;/code&gt; to get it running. You can then get rid of all the boilerplate&lt;br&gt;
code in the index file and you can put something like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;h1&amp;gt;Stripe Payments&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;A consultancy session&amp;lt;/p&amp;gt;
      &amp;lt;a
        href="https://buy.stripe.com/test_00g7v64lTd9EfVS9AB"
        className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
      &amp;gt;
        Buy Now
      &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2 - Create a Stripe Account
&lt;/h2&gt;

&lt;p&gt;This is pretty simple, just go to &lt;a href="https://stripe.com" rel="noopener noreferrer"&gt;stripe.com&lt;/a&gt; to do this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3 - Navigate to Stripe Dashboard
&lt;/h2&gt;

&lt;p&gt;Once your account is created, make sure you're on the stripe dashboard and enable test mode (toggle button in the top right). Then navigate to the payments page.&lt;br&gt;
From here you'll want to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose the option to create a payment link.&lt;/li&gt;
&lt;li&gt;Choose all of the options you want to collect from the customer.&lt;/li&gt;
&lt;li&gt;Configure the after payment option. (Note: if you're using localhost to launch the stripe checkout session and set localhost:3000 (or whatever port) as the place you wish to return to in development, you will probably run into issues. The only way that I've had this work is launching from 127.0.0.1:3000 and setting the redirect to 127.0.0.1.)&lt;/li&gt;
&lt;li&gt;Create the product which you're trying to sell.&lt;/li&gt;
&lt;li&gt;And finally create the payment link.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then copy the link and navigate back to your code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4 - Put the Link in Your code
&lt;/h2&gt;

&lt;p&gt;Now is when you want to insert a link into your code on the page where you are trying to sell a product and use this link as the &lt;code&gt;href&lt;/code&gt; value.&lt;br&gt;
This is all the work you will have to do on the front end. I suggest making things look pretty, but that is just an addition at this point&lt;br&gt;
(check out my website &lt;a href="https://revesta.net/pricing" rel="noopener noreferrer"&gt;revesta.net&lt;/a&gt; if you need some inspiration ;) ).&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5 - Setting up the Backend
&lt;/h2&gt;

&lt;p&gt;Before getting started with this, make sure that you have the Stripe CLI installed. &lt;a href="https://stripe.com/docs/stripe-cli" rel="noopener noreferrer"&gt;Click me&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to the &lt;code&gt;api&lt;/code&gt; folder in your project and create a new file called &lt;code&gt;webhook.ts&lt;/code&gt; (if you're using typescript).&lt;/li&gt;
&lt;li&gt;Install the packages &lt;code&gt;stripe&lt;/code&gt; and &lt;code&gt;raw-body&lt;/code&gt; (ie &lt;code&gt;npm i stripe raw-body&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Create a new file called &lt;code&gt;.env.local&lt;/code&gt; in the root of your project and insert the following 2 variables.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STRIPE_SECRET_KEY=
WEBHOOK_SECRET=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;You can get the stripe secret key by navigating to your dashboard, looking for API Keys and then it should be hidden. Just unhide it and copy it, then paste it in the &lt;code&gt;.env&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Now we can start to code in &lt;code&gt;webhook.ts&lt;/code&gt; so go to that file and insert the following.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import getRawBody from "raw-body";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
  apiVersion: "2023-10-16",
});

const endpointSecret = process.env.WEBHOOK_SECRET as string;

// Make sure to add this, otherwise you will get a stream.not.readable error
export const config = {
  api: {
    bodyParser: false,
  },
};

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    console.log("req.headers", req.headers);
    if (req.method !== "POST")
      return res.status(405).send("Only POST requests allowed");

    const sig: any = req.headers["stripe-signature"];
    const rawBody = await getRawBody(req);

    let event;

    try {
      event = stripe.webhooks.constructEvent(rawBody, sig, endpointSecret);
    } catch (err: any) {
      return res.status(400).send(`Webhook Error: ${err.message}`);
    }

    console.log("event.type", JSON.stringify(event.type));

    if (event.type === "checkout.session.completed") {
      const sessionWithLineItems = await stripe.checkout.sessions.retrieve(
        (event.data.object as any).id,
        {
          expand: ["line_items"],
        }
      );
      const lineItems = sessionWithLineItems.line_items;

      if (!lineItems) return res.status(500).send("Internal Server Error");

      try {
        // Save the data, change customer account info, etc
        console.log("Fullfill the order with custom logic");
        console.log("data", lineItems.data);
        console.log(
          "customer email",
          (event.data.object as any).customer_details.email
        );
        console.log("created", (event.data.object as any).created);
      } catch (error) {
        console.log("Handling when you're unable to save an order");
      }
    }

    res.status(200).end();
  } catch (error) {
    console.error(error);
    res.status(500).json("Internal Server Error");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To summarize, we are creating a route for stripe to send us events to. This is where we will see a checkout was successfully completed&lt;br&gt;
and handle it according (which is the last try block with all of the &lt;code&gt;console.logs&lt;/code&gt;). You probably know how to read code, so I'm not going&lt;br&gt;
to go over everything, but the main points are that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to make sure this route only accepts POST requests.&lt;/li&gt;
&lt;li&gt;We need to make sure that the request was sent by stripe.&lt;/li&gt;
&lt;li&gt;We need to handle a successful checkout.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 6 - Forwarding Hooks
&lt;/h2&gt;

&lt;p&gt;Open the terminal and enter in the following command &lt;code&gt;stripe listen --forward-to localhost:3000/api/webhook&lt;/code&gt;. This is telling stripe to send events to the route we just created. !Note that you should only do it this way in development.&lt;br&gt;
This will give you a webhook secret. Copy this secret and assign it to &lt;code&gt;WEBHOOK_SECRET&lt;/code&gt; in your &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7 - Give it a rip
&lt;/h2&gt;

&lt;p&gt;Go to localhost:3000 and go through the checkout process. Use 4242 for all the credit card numbers, give it a date past the current date and enter in any address. If all goes well, the fake payment will go through.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8 - Going Live
&lt;/h2&gt;

&lt;p&gt;Going live is how you can actually get real money from paying customers. Back in the Stripe dashboard, just untoggle the test mode and it will take you to live mode.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go through the same process of creating products and getting your secret key.&lt;/li&gt;
&lt;li&gt;Swap out the links so that they use the test payment links in development and live payment links in production (ie -&amp;gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;process.env.NODE_ENV === "development"
      ? "https://buy.stripe.com/test_link"
      : "https://buy.stripe.com/live_link",

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Register a webhook endpoint that points to your prod webhook endpoint. &lt;a href="https://stripe.com/docs/development/dashboard/register-webhook" rel="noopener noreferrer"&gt;Register webhooks here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That should have you on your way. I hope you found this useful, and have a great day.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>stripe</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>MongoDB Equivalent of SELECT column FROM table</title>
      <dc:creator>Jeremy Persing</dc:creator>
      <pubDate>Sat, 24 Jun 2023 05:02:06 +0000</pubDate>
      <link>https://dev.to/jeremypersing/mongodb-equivalent-of-select-column-from-table-28j0</link>
      <guid>https://dev.to/jeremypersing/mongodb-equivalent-of-select-column-from-table-28j0</guid>
      <description>&lt;p&gt;I've gotten lost in my notes too many times so this is a reference for my future self and anyone else who needs this.&lt;/p&gt;

&lt;p&gt;In an aggregation pipeline, add the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$group {
    _id: null (or whatever),
    countries: {
        $addToSet: "$subCollection.countries"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will result in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  _id: null,
  countries: ["a", "b", "c", etc...]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>The Ultimate React Native Project | Apartments.com Clone</title>
      <dc:creator>Jeremy Persing</dc:creator>
      <pubDate>Sun, 07 May 2023 07:33:53 +0000</pubDate>
      <link>https://dev.to/jeremypersing/the-ultimate-react-native-project-apartmentscom-clone-1gfo</link>
      <guid>https://dev.to/jeremypersing/the-ultimate-react-native-project-apartmentscom-clone-1gfo</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=Sr0GjzA9iPc"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mxMd7GXA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/34422870/236663721-883e273d-29a6-4f79-aed9-f8f3c292e19c.png" alt="Check this video out" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1:&lt;/strong&gt; &lt;a href="https://www.youtube.com/watch?v=Sr0GjzA9iPc"&gt;https://www.youtube.com/watch?v=Sr0GjzA9iPc&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Part 2:&lt;/strong&gt; &lt;a href="https://www.youtube.com/watch?v=E_GvwJ5IOCk"&gt;https://www.youtube.com/watch?v=E_GvwJ5IOCk&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Prior to creating this project I had worked on a React Native/Expo project and noticed that most, if not all, of the video tutorials online used Firebase in their implementation of things. I found this to be annoying at times, because my team wasn't using Firebase and there was no variety in tutorials for implementing things. Also, finding a genuine implementation for Expo Push Notifications on YouTube was impossible at that time. Luckily the expo documentation had everything that we needed, but at the end of that experience I thought that it would be useful to show that you don't just have to use Firebase when building out your React Native project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Project
&lt;/h2&gt;

&lt;p&gt;The app that I drew inspiration from was Apartments.com's mobile app. Prior to making the project, I had seen that they were able to sell their business for $500 - 600 million in the mid 2010's, yet when you look at their app it is relatively simple. Of course the value of the business probably had more to do with the number of users, revenue generated, and the data they had. But the point still remained that building out an app similar to theirs was very feasible. This video is me going through that process, and showing you how you can do the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You'll Implement
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;The project uses the expo SDK 45, and the current version is 48. Implementations might differ slightly now (idk), but the fundamental concepts should still work. Check out the expo docs if you get stuck &lt;a href="https://docs.expo.dev/"&gt;https://docs.expo.dev/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Client Side
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement Push Notifications&lt;/li&gt;
&lt;li&gt;Implement Maps into your project&lt;/li&gt;
&lt;li&gt;Login with Google&lt;/li&gt;
&lt;li&gt;Login with Facebook&lt;/li&gt;
&lt;li&gt;Login with Apple&lt;/li&gt;
&lt;li&gt;Login and Register with an email and password&lt;/li&gt;
&lt;li&gt;Implement Forgot User Password and Reset User Password Screens&lt;/li&gt;
&lt;li&gt;Implement star based reviews&lt;/li&gt;
&lt;li&gt;Use React Query for fetching data&lt;/li&gt;
&lt;li&gt;Implement chat/messaging screens&lt;/li&gt;
&lt;li&gt;Create Detailed Screens to display data&lt;/li&gt;
&lt;li&gt;Create Airbnb like carousel cards for displaying apartment images&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Backend / Database
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Build an API in GO&lt;/li&gt;
&lt;li&gt;Implement and use Postgresql&lt;/li&gt;
&lt;li&gt;Send notifications to users from your server&lt;/li&gt;
&lt;li&gt;Upload and store images to an S3 Bucket&lt;/li&gt;
&lt;li&gt;Implement Chat into your application using Socket.io&lt;/li&gt;
&lt;li&gt;Use nodejs to create a chat server&lt;/li&gt;
&lt;li&gt;Implement redis&lt;/li&gt;
&lt;li&gt;Login with Google&lt;/li&gt;
&lt;li&gt;Login with Facebook&lt;/li&gt;
&lt;li&gt;Login with Apple&lt;/li&gt;
&lt;li&gt;Login and Register with an email and password&lt;/li&gt;
&lt;li&gt;Use JWTs&lt;/li&gt;
&lt;li&gt;Deploy servers using Docker and Dockerhub&lt;/li&gt;
&lt;li&gt;Create a domain for free &lt;/li&gt;
&lt;li&gt;Use NGINX and Letsencrypt to enable https connections with your server&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Github
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Client: &lt;a href="https://github.com/JeremyPersing/apartmentsclone"&gt;https://github.com/JeremyPersing/apartmentsclone&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Server: &lt;a href="https://github.com/JeremyPersing/apartmentscloneserver"&gt;https://github.com/JeremyPersing/apartmentscloneserver&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Chat Server: &lt;a href="https://github.com/JeremyPersing/apartmentsclonechatserver"&gt;https://github.com/JeremyPersing/apartmentsclonechatserver&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;A few of the screens you'll build&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AE3GL8Lt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/34422870/236635898-118a242f-9973-4b5a-be0c-9b74c90ffb04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AE3GL8Lt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/34422870/236635898-118a242f-9973-4b5a-be0c-9b74c90ffb04.png" alt="1" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_qfnU83p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/34422870/236635894-cdf6b98c-f9c2-4d9e-ab6b-63d3d71ba642.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_qfnU83p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/34422870/236635894-cdf6b98c-f9c2-4d9e-ab6b-63d3d71ba642.png" alt="2" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Add Country Flags to Your App With This API - countryflags.io alternative</title>
      <dc:creator>Jeremy Persing</dc:creator>
      <pubDate>Tue, 21 Dec 2021 20:53:40 +0000</pubDate>
      <link>https://dev.to/jeremypersing/add-country-flags-to-your-app-with-this-api-countryflagsio-alternative-d0d</link>
      <guid>https://dev.to/jeremypersing/add-country-flags-to-your-app-with-this-api-countryflagsio-alternative-d0d</guid>
      <description>&lt;p&gt;&lt;em&gt;I am going to be shutting down this service as of 3/12/23. If you would like to run your own instance of this, go to the &lt;a href="https://github.com/JeremyPersing/countryflagsapi" rel="noopener noreferrer"&gt;country flags repository&lt;/a&gt;, download the code and run it on your own VPS.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I was recently working on a project that required me to implement country flags into the application. At the time, countryflags.io was a popular API that people were using, so I decided that I should rely on it for my project. While integrating the endpoints for countryflags.io into my application, I noticed that they only allowed user's to retrieve a country's flag by a two letter code. For example, if I wanted to get the flag of the United States, the endpoint would be &lt;code&gt;https://www.countryflags.io/us/flat/64.png&lt;/code&gt;. This was a problem because my data didn't identify countries by their two letter codes. Another problem I ran into is that the server of countryflags.io went down half way through the project. So to fix these problems, I created my own API that allows users to get a country's flag by the country's name, its two letter code (ISO Alpha 2 code), its three letter code (ISO Alpha 3 code), and its UN Code. The codes for each country can be found at &lt;a href="https://www.countryflagsapi.com" rel="noopener noreferrer"&gt;countryflagsapi.com&lt;/a&gt;. Here are a few example endpoints &lt;br&gt;
&lt;a href="https://countryflagsapi.com/png/br" rel="noopener noreferrer"&gt;https://countryflagsapi.com/png/br&lt;/a&gt;&lt;br&gt;
&lt;a href="https://countryflagsapi.com/png/brazil" rel="noopener noreferrer"&gt;https://countryflagsapi.com/png/brazil&lt;/a&gt;&lt;br&gt;
&lt;a href="https://countryflagsapi.com/svg/076" rel="noopener noreferrer"&gt;https://countryflagsapi.com/svg/076&lt;/a&gt;&lt;br&gt;
&lt;a href="https://countryflagsapi.com/svg/bra" rel="noopener noreferrer"&gt;https://countryflagsapi.com/svg/bra&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Your Own Instance
&lt;/h2&gt;

&lt;p&gt;If you wish to run your own instance of this API, you can download the source code &lt;a href="https://github.com/JeremyPersing/countryflagsapi" rel="noopener noreferrer"&gt;here&lt;/a&gt;. If you would prefer to just pull a docker image of the API, you can get that &lt;a href="https://hub.docker.com/repository/docker/jeremypersing/countryflagsapi" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting a Flag from User Input
&lt;/h2&gt;

&lt;p&gt;I'll be using React for this demo. Keep in mind this is a basic demo, but this will probably be similar to what you will want&lt;br&gt;
to use in your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imports
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  States and Functions&lt;br&gt;
&lt;/h3&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flagURL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlagURL&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&lt;a href="https://countryflagsapi.com/png/cuba" rel="noopener noreferrer"&gt;https://countryflagsapi.com/png/cuba&lt;/a&gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIdentifier&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleButtonClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="c1"&gt;// can use &lt;a href="https://countryflagsapi.com/svg/" rel="noopener noreferrer"&gt;https://countryflagsapi.com/svg/&lt;/a&gt; here too&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&lt;a href="https://countryflagsapi.com/png/" rel="noopener noreferrer"&gt;https://countryflagsapi.com/png/&lt;/a&gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;br&gt;
  &lt;span class="nf"&gt;setFlagURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  JSX&lt;br&gt;
&lt;/h3&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;marginBottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;20px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;country&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setIdentifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter a country name, UN Code, ISO Alpha-2, or ISO Alpha-3 Code&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleButtonClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="nx"&gt;Flag&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flagURL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Embedding an Image&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Go to the flag that you want to embed and click the button labeled "Embed SVG" or "Embed PNG". A modal will then appear like the one shown in the image below. Go ahead and click copy and then paste the img element into your source code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l2imjjawnkmqy8qm16m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l2imjjawnkmqy8qm16m.png" alt="Embed an img element"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
